对于人脸识别,我们需要从mysql DB中读取向量并将其转换为一维数组以进行识别。
从mysql中检索以下对象,该对象以JSON格式存储在mysql中。
s1= [-0.12783311307430267, 0.13190957903862, 0.09596485644578934, -0.09712248295545578, -0.11780811846256256, 0.007217485923320055, -0.11222986876964568, -0.04876283556222915, 0.2216355949640274, -0.10998672991991044, 0.18791022896766665, 0.08736929297447205, -0.16356715559959412, -0.01818229630589485, -0.021847818046808243, 0.13041044771671295, -0.13738003373146057, -0.09671961516141891, -0.02172057516872883, -0.14230197668075562, 0.05242226645350456, 0.09029272198677064, -0.002328673377633095, 0.00397188076749444, -0.20264719426631927, -0.2703503370285034, -0.12626759707927704, -0.07806601375341415, 0.1267593950033188, -0.13493752479553223, -0.043157391250133514, -0.016596168279647827, -0.16287413239479065, -0.0656481683254242, 0.03958671912550926, 0.07427462935447693, -0.06913568824529648, -0.0629347711801529, 0.178871750831604, 0.08766687661409378, -0.09091204404830933, 0.011706589721143246, 0.04953945055603981, 0.27260822057724, 0.16323423385620117, 0.0253637433052063, 0.10930740833282472, -0.11138659715652466, 0.12710365653038025, -0.21879185736179352, 0.08703845739364624, 0.1319100558757782, 0.034353598952293396, 0.10928373038768768, 0.12201938033103944, -0.17505870759487152, 0.014886455610394478, 0.09721830487251282, -0.1538517326116562, 0.1672624796628952, 0.10802490264177322, -0.0300880316644907, -0.10113763064146042, -0.006332905497401953, 0.2025756984949112, 0.09179922193288804, -0.113119974732399, -0.20894797146320343, 0.15196490287780762, -0.12537285685539246, -0.05446625128388405, 0.11561021953821182, -0.11292491853237152, -0.11957288533449172, -0.2860523462295532, 0.08072254806756973, 0.4256277084350586, 0.15420164167881012, -0.11120182275772096, 0.0442705899477005, -0.06239746138453483, -0.043058544397354126, 0.03340786322951317, 0.06541167199611664, -0.12657742202281952, 0.017120834439992905, -0.03134886175394058, 0.10211298614740372, 0.20427383482456207, 0.03959937393665314, -0.01935926266014576, 0.2007918357849121, 0.051548801362514496, 0.08825849741697311, 0.016137562692165375, 0.1055104285478592, -0.15693385899066925, -0.07759833335876465, -0.0738161951303482, -0.05325906723737717, 0.08928476274013519, -0.09207655489444733, 0.035900089889764786, 0.17390793561935425, -0.17486083507537842, 0.20116952061653137, -0.03213610127568245, -0.03827217221260071, -0.05482873693108559, 0.07908175885677338, -0.14436912536621094, -0.010803371667861938, 0.1578456461429596, -0.2274101823568344, 0.18742960691452024, 0.2068897932767868, 0.04828779026865959, 0.08840855956077576, 0.11564983427524568, 0.051258377730846405, 0.0017237504944205284, 0.01801629178225994, -0.0783238410949707, -0.07523202151060104, -0.05354651063680649, -0.06916598975658417, 0.04159272089600563, 0.03667797893285751]
b = str(s1)
as1=np.fromstring(b, dtype=np.uint8, count=-1,sep=',')
print '-output--',as1
np.reshape(as1, (-1,1)
它正在返回array([], dtype=uint8)
它尝试了以下所有选项,但无法获取
答案 0 :(得分:0)
如果在您的示例中,字符串表示形式恰好反映了常规Python列表的字符串表示形式,则可以在输入type Store struct {
StoreID int `gorm:"primary_key;AUTO_INCREMENT;not null"`
Name string `gorm:"not null"`
Adress string `gorm:"not null"`
Manager User `gorm:"not null"`
ManagerID int `gorm:"foreignkey:ManagerID;not null"`
Boxes []Box `gorm:"foreignkey:StoreID;association_foreignkey:StoreID"`
}
type User struct {
UserID int `json:"id" gorm:"primary_key;AUTO_INCREMENT;not null"`
Username string `json:"username" gorm:"not null"`
Password string `json:"password" gorm:"not null"`
Email string `json:"email" gorm:"not null"`
Right UserRight `json:"userright" gorm:"not null"`
}
type Box struct {
BoxID int `gorm:"primary_key;AUTO_INCREMENT;not null"`
StoreID int `gorm:"not null"`
Code int `gorm:"type:integer(13)"`
Description string `gorm:"not null"`
}
之前使用ast.literal_eval
:
np.array
文档解释了literal_eval
接受的类型:
安全地评估表达式节点或包含Python的字符串 文字或容器显示。提供的字符串或节点只能 由以下Python文字结构组成:字符串,字节, 数字,元组,列表,字典,集合,布尔值和
from ast import literal_eval as1 = np.array(literal_eval(b), dtype=np.uint8) print(as1.dtype) # uint8
。
因此,我们正在有效地将字符串转换为Python None
,然后list
可以将其转换为NumPy数组。
如果您热衷于使用np.array
,请注意,不包括方括号和右方括号:
np.fromstring
请注意,如上所述,在转换为as1 = np.fromstring(b[1:-1], sep=',').astype(np.uint8)
之前,您需要先读为float
(默认设置)。在这种情况下,您应该期望所有0值。
答案 1 :(得分:0)
尝试一下
b = str(s1) # converting to string as your input is string
output = np.fromstring(b[1:-1],sep=',') # using b[1:-1] to get the string alone by slicing square braces ([])
print(output)
检查doc以获得更多信息