我有一个编码的图像,我想把它存储在一个数据库里
编码后的图像看起来像
[array([-0.06772001, 0.05728862, -0.0251427 , -0.08454875, -0.03213117,
-0.03106732, 0.00076004, -0.06710728, 0.13713066, -0.11327504,
0.21360487, -0.09900057, -0.22535393, -0.05674654, -0.0756731 ,
0.05203095, -0.15146038, -0.0874744 , -0.10700339, -0.06584571,
0.07124957, 0.04297648, 0.00613243, 0.10702223, -0.18388851,
-0.29916996, -0.02585445, -0.10655323, 0.02479932, -0.1107952 ,
0.05695343, 0.15145421, -0.14358994, 0.01052327, -0.04699658,
0.05112756, -0.01958742, -0.13124494, 0.22351149, 0.08037129,
-0.14257275, -0.00716499, -0.04201137, 0.30405453, 0.09107384,
-0.02807541, 0.06258672, -0.05632804, 0.1744605 , -0.22389032,
-0.00614528, 0.19390592, 0.13520201, 0.02655653, 0.05437692,
-0.12989879, 0.11905471, 0.167327 , -0.2371735 , 0.0535943 ,
0.02655954, -0.02821845, 0.00393433, -0.01588794, 0.08551847,
0.05382346, -0.08446158, -0.04891751, 0.13232884, -0.11974856,
-0.01156278, 0.1275105 , -0.10302921, -0.28067535, -0.13956669,
0.08966827, 0.35788029, 0.2560949 , -0.15698224, 0.00970889,
-0.05089435, -0.03781725, 0.13893284, 0.06806683, -0.11791685,
-0.06655203, -0.1322563 , 0.05103128, 0.14470096, 0.0419168 ,
-0.06990704, 0.22407535, 0.0648756 , -0.07097621, 0.008138 ,
-0.0258901 , -0.16763684, 0.09151821, -0.0178766 , 0.02473853,
0.10130395, -0.08122514, -0.0450197 , 0.05235555, -0.154888 ,
0.19613147, -0.02572119, -0.06538303, 0.01294655, -0.03529175,
-0.05470725, 0.00442413, 0.09804396, -0.23483986, 0.05548284,
0.13926584, 0.07040435, 0.21667242, 0.07278855, 0.06018258,
0.02701348, -0.04436196, -0.16826543, -0.12570928, 0.11218779,
0.02221176, 0.04686672, -0.07655797])]
但是当我从数据库中查看或选择*时,输出是这样的
(b"\x00\x00\x00@\x19V\xb1\xbf\x00\x00\x00\x00\xefT\xad?\x00\x00\x00@\x02\xbf\x99\xbf\x00\x00\x00\xa0\xfc\xa4\xb5\xbf\x00\x00\x00\x00\x7fs\xa0\xbf\x00\x00\x00`\x1c\xd0\x9f\xbf\x00\x00\x00\x00\xac\xe7H?\x00\x00\x00@\xf1-\xb1\xbf\x00\x00\x00`\x7f\x8d\xc1?\x00\x00\x00\xc0\x97\xff\xbc\xbf\x00\x00\x00\x80gW\xcb?\x00\x00\x00\xe0\x19X\xb9\xbf\x00\x00\x00\xc0e\xd8\xcc\xbf\x00\x00\x00\xe0\xe1\r\xad\xbf\x00\x00\x00\x00P_\xb3\xbf\x00\x00\x00\xe0\xcc\xa3\xaa?\x00\x00\x00\xc0\rc\xc3\xbf\x00\x00\x00\xe0\xb8d\xb6\xbf\x00\x00\x00\x00\x93d\xbb\xbf\x00\x00\x00\xa0C\xdb\xb0\xbf\x00\x00\x00\x80i=\xb2?\x00\x00\x00\x80\x03\x01\xa6?\x00\x00\x00\xc0Q\x1ey?\x00\x00\x00 \xcfe\xbb?\x00\x00\x00\xa0\xa8\x89\xc7\xbf\x00\x00\x00\xc0\x99%\xd3\xbf\x00\x00\x00\x00\x97y\x9a\xbf\x00\x00\x00\x80\x12G\xbb\xbf\x00\x00\x00\x80\xfed\x99?\x00\x00\x00\x00\x13]\xbc\xbf\x00\x00\x00\x00\x00)\xad?\x00\x00\x00\x00\xdab\xc3?\x00\x00\x00\xc0'a\xc2\xbf\x00\x00\x00\xc08\x8d\x85?\x00\x00\x00\x80\xef\x0f\xa8\xbf\x00\x00\x00`d-\xaa?\x00\x00\x00\x80\xb9\x0e\x94\xbf\x00\x00\x00`\xa2\xcc\xc0\xbf\x00\x00\x00@\x06\x9c\xcc?\x00\x00\x00\x806\x93\xb4?\x00\x00\x00\xe0\xd2?\xc2\xbf\x00\x00\x00@\x08Y}\xbf\x00\x00\x00\xc0\x83\x82\xa5\xbf\x00\x00\x00 \xa1u\xd3?\x00\x00\x00\x80\x9dP\xb7?\x00\x00\x00\xe0\xcc\xbf\x9c\xbf\x00\x00\x00\x00\xaf\x05\xb0?\x00\x00\x00@\x07\xd7\xac\xbf\x00\x00\x00\xc0\xb8T\xc6?\x00\x00\x00 p\xa8\xcc\xbf\x00\x00\x00\x00\xca+y\xbf\x00\x00\x00\xc0\xe8\xd1\xc8?\x00\x00\x00\xa0LN\xc1?\x00\x00\x00@\xa21\x9b?\x00\x00\x00\xc0J\xd7\xab?\x00\x00\x00\x00\x86\xa0\xc0\xbf\x00\x00\x00\xa0^z\xbe?\x00\x00\x00\xa0\xf8j\xc5?\x00\x00\x00\x80\xb3[\xce\xbf\x00\x00\x00@\xb6p\xab?\x00\x00\x00\x80l2\x9b?\x00\x00\x00\xe0K\xe5\x9c\xbf\x00\x00\x00\x80p\x1dp?\x00\x00\x00\x80\xedD\x90\xbf\x00\x00\x00\xe0\x89\xe4\xb5?\x00\x00\x00\xa0\xbf\x8e\xab?\x00\x00\x00@F\x9f\xb5\xbf\x00\x00\x00@\xb7\x0b\xa9\xbf\x00\x00\x00\xc0&\xf0\xc0?\x00\x00\x00\x80\xd7\xa7\xbe\xbf\x00\x00\x00\x00:\xae\x87\xbf\x00\x00\x00\xa0CR\xc0?\x00\x00\x00@\x1f`\xba\xbf\x00\x00\x00\xc0\x95\xf6\xd1\xbf\x00\x00\x00@R\xdd\xc1\xbf\x00\x00\x00\x00\x80\xf4\xb6?\x00\x00\x00\xc0\x82\xe7\xd6?\x00\x00\x00\xe0\xdbc\xd0?\x00\x00\x00\x80\xfe\x17\xc4\xbf\x00\x00\x00 A\xe2\x83?\x00\x00\x00\xe0\xd2\x0e\xaa\xbf\x00\x00\x00`\xc8\\\xa3\xbf\x00\x00\x00 \x8d\xc8\xc1?\x00\x00\x00\xe0\xd3l\xb1?\x00\x00\x00\x80\xcc/\xbe\xbf\x00\x00\x00\xc0\x8d\t\xb1\xbf\x00\x00\x00@\xc6\xed\xc0\xbf\x00\x00\x00\x80\xc5 \xaa?\x00\x00\x00\xa0\x8f\x85\xc2?\x00\x00\x00\x80\x1ev\xa5?\x00\x00\x00\x80m\xe5\xb1\xbf\x00\x00\x00@\x80\xae\xcc?\x00\x00\x00\xe0\xaf\x9b\xb0?\x00\x00\x00@\x7f+\xb2\xbf\x00\x00\x00\x80\xa7\xaa\x80?\x00\x00\x00\xe0\xee\x82\x9a\xbf\x00\x00\x00\xc0\x1fu\xc5\xbf\x00\x00\x00\xc0\xbcm\xb7?\x00\x00\x00@>N\x92\xbf\x00\x00\x00\x80\x0eU\x99?\x00\x00\x00@\x0e\xef\xb9?\x00\x00\x00\xc0+\xcb\xb4\xbf\x00\x00\x00`\xd2\x0c\xa7\xbf\x00\x00\x00\xc0X\xce\xaa?\x00\x00\x00\xc0^\xd3\xc3\xbf\x00\x00\x00\x00\xd6\x1a\xc9?\x00\x00\x00\xc0\xa7V\x9a\xbf\x00\x00\x00@\xf1\xbc\xb0\xbf\x00\x00\x00\xa0\xb8\x83\x8a?\x00\x00\x00\x80\xc2\x11\xa2\xbf\x00\x00\x00\xc0\x96\x02\xac\xbf\x00\x00\x00\x00\t\x1fr?\x00\x00\x00\xa0h\x19\xb9?\x00\x00\x00\x80;\x0f\xce\xbf\x00\x00\x00@?h\xac?\x00\x00\x00\x80v\xd3\xc1?\x00\x00\x00\x00\x05\x06\xb2?\x00\x00\x00\x00\xec\xbb\xcb?\x00\x00\x00@E\xa2\xb2?\x00\x00\x00`@\xd0\xae?\x00\x00\x00\xa0k\xa9\x9b?\x00\x00\x00\x80\x9c\xb6\xa6\xbf\x00\x00\x00\xc0\xb8\x89\xc5\xbf\x00\x00\x00\xe0=\x17\xc0\xbf\x00\x00\x00\xc0V\xb8\xbc?\x00\x00\x00\x00\xae\xbe\x96?\x00\x00\x00@\xea\xfe\xa7?\x00\x00\x00\xa0M\x99\xb3\xbf",)
我用两个不同的代码做了,结果是一样的
代码nom1
x=face_recognition.face_encodings(new_picture)
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String
engine = create_engine('sqlite:///image.db', echo = True)
meta = MetaData()
img= Table(
'img', meta,
Column('id', Integer, primary_key = True),
Column('imagecode', String),
)
meta.create_all(engine)
engine.execute("insert into students (name) values (?)",(x,))
代码nom2
conn = sqlite3.connect('image.db')
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS img(keyword TEXT)")
c.execute("insert into image(imagecode) values (?)",(x))
conn.commit()
c.close()
conn.close()
那么我怎样才能把它转换成原始图像代码。
如果您想存储这样的对象,您应该将其序列化,并将其存储为BLOB(二进制大型对象):
import array
import pickle
import sqlite3
cnxn = sqlite3.connect(":memory:")
crsr = cnxn.cursor()
crsr.execute("CREATE TABLE image (id int primary key, imagecode varbinary)")
original = [array.array("d", [-0.123, -0.456])]
sql = "INSERT INTO image (id, imagecode) VALUES (1, ?)"
params = (pickle.dumps(original), )
crsr.execute(sql, params)
sql = "SELECT imagecode FROM image WHERE id = 1"
retrieved = pickle.loads(crsr.execute(sql).fetchone()[0])
print(retrieved) # [array('d', [-0.123, -0.456])]