简体   繁体   中英

Unable to convert String to numpy 1d array

For facial recognition, we need to read vector from mysql DB and convert to 1D array for recognition.

The following object is retrieved from mysql which is stored as JSON format in 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)

it is returning array([], dtype=uint8)

It tried all the below options but could not get it

Convert string to numpy array

Convert string array to numpy array

If, as in your example, the string representation precisely reflects that of a regular Python list, you can use ast.literal_eval before feeding np.array :

from ast import literal_eval

as1 = np.array(literal_eval(b), dtype=np.uint8)

print(as1.dtype)

# uint8

The docs explain types accepted by literal_eval :

Safely evaluate an expression node or a string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None .

So we are effectively converting a string to a Python list , which np.array can then convert to a NumPy array.


If you are keen on using np.fromstring , note that open and close square brackets must be excluded:

as1 = np.fromstring(b[1:-1], sep=',').astype(np.uint8)

Note, as above, you will need to read as float first (the default) before conversion to np.uint8 . In this case, you should expect all 0 values.

Try this 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)

check the doc for more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM