classify.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import base64
  2. from video_to_photo import run
  3. import os
  4. import advice
  5. def get_base64(path):
  6. with open(path, "rb") as f:
  7. base64_data = base64.b64encode(f.read())
  8. return base64_data.decode()
  9. def classify_jump(name, re):
  10. q = "./capture_image/" + name
  11. qqq = os.path.abspath(q)
  12. result = {}
  13. result["jump_power"] = {}
  14. if (float(re["1"]["val"]) > 140.0):
  15. result["jump_power"]["level"] = 1
  16. elif (float(re["1"]["val"]) > 115.0):
  17. result["jump_power"]["level"] = 2
  18. elif (float(re["1"]["val"]) > 90.0):
  19. result["jump_power"]["level"] = 3
  20. else:
  21. result["jump_power"]["level"] = 4
  22. r = "capture_image_result" + str(re["1"]["num"]) + ".png"
  23. a = os.path.join(qqq, r)
  24. # result["jump_power"]["path"]=get_base64(a)
  25. result["jump_power"]["val"] = re["1"]["val"]
  26. result["core_strength"] = {}
  27. if (float(re["2"]["val"]) < 8.0):
  28. result["core_strength"]["level"] = 1
  29. elif (float(re["2"]["val"]) < 15.0):
  30. result["core_strength"]["level"] = 2
  31. elif (float(re["2"]["val"]) < 30.0):
  32. result["core_strength"]["level"] = 3
  33. else:
  34. result["core_strength"]["level"] = 4
  35. r = "capture_image_result" + str(re["2"]["num"]) + ".png"
  36. a = os.path.join(qqq, r)
  37. # result["core_strength"]["path"]=get_base64(a)
  38. result["landing_position"] = {}
  39. if (float(re["3"]["val"]) > 140.0):
  40. result["landing_position"]["level"] = 1
  41. elif (float(re["3"]["val"]) > 115.0):
  42. result["landing_position"]["level"] = 2
  43. elif (float(re["3"]["val"]) > 90.0):
  44. result["landing_position"]["level"] = 3
  45. else:
  46. result["landing_position"]["level"] = 4
  47. r = "capture_image_result" + str(re["3"]["num"]) + ".png"
  48. a = os.path.join(qqq, r)
  49. # result["landing_position"]["path"]=get_base64(a)
  50. result["landing_position"]["val"] = re["3"]["val"]
  51. result["advice"] = advice.ad_jump(result)
  52. res = []
  53. d = {}
  54. d["item"] = "jump_power"
  55. d["a"] = str(5 - int(result["jump_power"]["level"]))
  56. d["val"] = result["jump_power"]["val"]
  57. # d["path"]=result["arm_strength"]["path"]
  58. res.append(d)
  59. d = {}
  60. d["item"] = "core_strength"
  61. d["a"] = 5 - int(result["core_strength"]["level"])
  62. # d["path"]=result["stability"]["path"]
  63. res.append(d)
  64. d = {}
  65. d["item"] = "landing_position"
  66. d["a"] = 5 - int(result["landing_position"]["level"])
  67. d["val"] = result["landing_position"]["val"]
  68. # d["path"]=result["standard"]["path"]
  69. res.append(d)
  70. # d={}
  71. # d["item"]="advice"
  72. # d["a"]=result["advice"]
  73. # res.append(d)
  74. return {"analyzed_data": res, "advice": result["advice"]}
  75. def classify_juanfu(name, re):
  76. q = "./capture_image/" + name
  77. qqq = os.path.abspath(q)
  78. result = {}
  79. result["head_power"] = {}
  80. if (float(re["1"]["val"]) > 100.0):
  81. result["head_power"]["level"] = 1
  82. elif (float(re["1"]["val"]) > 90.0):
  83. result["head_power"]["level"] = 2
  84. elif (float(re["1"]["val"]) > 80.0):
  85. result["head_power"]["level"] = 3
  86. else:
  87. result["head_power"]["level"] = 4
  88. r = "capture_image_result" + str(re["1"]["num"]) + ".png"
  89. a = os.path.join(qqq, r)
  90. # result["head_power"]["path"]=get_base64(a)
  91. result["head_power"]["val"] = re["1"]["val"]
  92. result["core_strength"] = {}
  93. if (float(re["2"]["val"]) > 90):
  94. result["core_strength"]["level"] = 1
  95. elif (float(re["2"]["val"]) > 80):
  96. result["core_strength"]["level"] = 2
  97. elif (float(re["2"]["val"]) > 70):
  98. result["core_strength"]["level"] = 3
  99. else:
  100. result["core_strength"]["level"] = 4
  101. r = "capture_image_result" + str(re["2"]["num"]) + ".png"
  102. a = os.path.join(qqq, r)
  103. result["core_strength"]["path"] = get_base64(a)
  104. result["body_stability"] = {}
  105. if (float(re["4"]["num"]) < 15):
  106. result["body_stability"]["level"] = 1
  107. elif (float(re["4"]["num"]) < 20):
  108. result["body_stability"]["level"] = 2
  109. elif (float(re["4"]["num"]) < 30):
  110. result["body_stability"]["level"] = 3
  111. else:
  112. result["body_stability"]["level"] = 4
  113. result["body_stability"]["val"] = re["4"]["num"]
  114. result["advice"] = advice.ad_juanfu(result)
  115. res = []
  116. d = {}
  117. d["item"] = "head_power"
  118. d["a"] = 5 - int(result["head_power"]["level"])
  119. d["val"] = result["head_power"]["val"]
  120. # d["path"]=result["arm_strength"]["path"]
  121. res.append(d)
  122. d = {}
  123. d["item"] = "core_strength"
  124. d["a"] = 5 - int(result["core_strength"]["level"])
  125. # d["path"]=result["stability"]["path"]
  126. res.append(d)
  127. d = {}
  128. d["item"] = "body_stability"
  129. d["a"] = 5 - int(result["body_stability"]["level"])
  130. d["val"] = result["body_stability"]["val"]
  131. # d["path"]=result["standard"]["path"]
  132. res.append(d)
  133. # d={}
  134. # d["item"]="advice"
  135. # d["a"]=result["advice"]
  136. # res.append(d)
  137. return {"analyzed_data": res, "advice": result["advice"]}
  138. def classify_pingban(name, re):
  139. q = "./capture_image/" + name
  140. qqq = os.path.abspath(q)
  141. result = {}
  142. result["arm_strength"] = {}
  143. dis = abs(90 - float(re["1"]["val"]))
  144. if (dis < 5):
  145. result["arm_strength"]["level"] = 1
  146. elif (dis < 10):
  147. result["arm_strength"]["level"] = 2
  148. elif (dis < 20):
  149. result["arm_strength"]["level"] = 3
  150. else:
  151. result["arm_strength"]["level"] = 4
  152. r = "capture_image_result" + str(re["1"]["num"]) + ".png"
  153. a = os.path.join(qqq, r)
  154. # result["arm_strength"]["path"]=get_base64(a)
  155. result["arm_strength"]["val"] = re["1"]["val"]
  156. result["stability"] = {}
  157. if (float(re["2"]["val"]) < 5):
  158. result["stability"]["level"] = 1
  159. elif (float(re["2"]["val"]) < 10):
  160. result["stability"]["level"] = 2
  161. elif (float(re["2"]["val"]) < 15):
  162. result["stability"]["level"] = 3
  163. else:
  164. result["stability"]["level"] = 4
  165. r = "capture_image_result" + str(re["2"]["num"]) + ".png"
  166. a = os.path.join(qqq, r)
  167. # result["stability"]["path"]=get_base64(a)
  168. result["stability"]["val"] = re["2"]["val"]
  169. result["standard"] = {}
  170. if (float(re["3"]["val"]) < 5):
  171. result["standard"]["level"] = 1
  172. elif (float(re["3"]["val"]) < 10):
  173. result["standard"]["level"] = 2
  174. elif (float(re["3"]["val"]) < 15):
  175. result["standard"]["level"] = 3
  176. else:
  177. result["standard"]["level"] = 4
  178. r = "capture_image_result" + str(re["3"]["num"]) + ".png"
  179. a = os.path.join(qqq, r)
  180. result["standard"]["path"] = get_base64(a)
  181. result["standard"]["val"] = re["3"]["val"]
  182. result["advice"] = advice.ad_pingban(result)
  183. res = []
  184. d = {}
  185. d["item"] = "arm_arm_strength"
  186. d["a"] = 5 - int(result["arm_strength"]["level"])
  187. d["val"] = result["arm_strength"]["val"]
  188. # d["path"]=result["arm_strength"]["path"]
  189. res.append(d)
  190. d = {}
  191. d["item"] = "stability"
  192. d["a"] = 5 - int(result["stability"]["level"])
  193. d["val"] = result["stability"]["val"]
  194. # d["path"]=result["stability"]["path"]
  195. res.append(d)
  196. d = {}
  197. d["item"] = "standard"
  198. d["a"] = 5 - int(result["standard"]["level"])
  199. d["val"] = result["standard"]["val"]
  200. # d["path"]=result["standard"]["path"]
  201. res.append(d)
  202. # d={}
  203. # d["item"]="advice"
  204. # d["a"]=result["advice"]
  205. # res.append(d)
  206. return {"analyzed_data": res, "advice": result["advice"]}
  207. def classify_shendun(name, re):
  208. q = "./capture_image/" + name
  209. qqq = os.path.abspath(q)
  210. result = {}
  211. result["leg_strength"] = {}
  212. if (float(re["1"]["val"]) < 60):
  213. result["leg_strength"]["level"] = 1
  214. elif (float(re["1"]["val"]) < 80):
  215. result["leg_strength"]["level"] = 2
  216. elif (float(re["1"]["val"]) < 90):
  217. result["leg_strength"]["level"] = 3
  218. else:
  219. result["leg_strength"]["level"] = 4
  220. r = "capture_image_result" + str(re["1"]["num"]) + ".png"
  221. a = os.path.join(qqq, r)
  222. # result["leg_strength"]["path"]=get_base64(a)
  223. result["advice"] = advice.ad_shendun(result)
  224. # return result
  225. res = []
  226. d = {}
  227. d["item"] = "leg_strengthh"
  228. d["a"] = 5 - int(result["leg_strength"]["level"])
  229. # d["val"]=result["arm_strength"]["val"]
  230. # d["path"]=result["arm_strength"]["path"]
  231. res.append(d)
  232. # d={}
  233. # d["item"]="advice"
  234. # d["a"]=result["advice"]
  235. # res.append(d)
  236. return {"analyzed_data": res, "advice": result["advice"]}
  237. def classify_gaotaitui(name, re):
  238. q = "./capture_image/" + name
  239. qqq = os.path.abspath(q)
  240. result = {}
  241. result["standard"] = {}
  242. if (float(re["1"]["val"]) < 40):
  243. result["standard"]["level"] = 1
  244. elif (float(re["1"]["val"]) < 70):
  245. result["standard"]["level"] = 2
  246. elif (float(re["1"]["val"]) < 100):
  247. result["standard"]["level"] = 3
  248. else:
  249. result["standard"]["level"] = 4
  250. r = "capture_image_result" + str(re["1"]["num"]) + ".png"
  251. a = os.path.join(qqq, r)
  252. # result["standard"]["path"]=get_base64(a)
  253. result["advice"] = advice.ad_gaotaitui(result)
  254. # return result
  255. #
  256. res = []
  257. d = {}
  258. d["item"] = "standard"
  259. d["a"] = 5 - int(result["standard"]["level"])
  260. # d["val"]=result["standard"]["val"]
  261. # d["path"]=result["arm_strength"]["path"]
  262. res.append(d)
  263. # d={}
  264. # d["item"]="advice"
  265. # d["a"]=result["advice"]
  266. # res.append(d)
  267. return {"analyzed_data": res, "advice": result["advice"]}
  268. def classify_result(video_path, re, mode):
  269. # video_path = "./video/demo1.mp4"
  270. path = video_path
  271. os.path.split(path)
  272. tur = os.path.split(path)
  273. name = tur[len(tur) - 1].split('.')[0]
  274. switch = {'jump': classify_jump,
  275. 'pingban': classify_pingban,
  276. 'juanfu': classify_juanfu,
  277. 'shendun': classify_shendun,
  278. 'gaotaitui': classify_gaotaitui,
  279. }
  280. choice = mode
  281. power = switch.get(choice)(name, re)
  282. print(power)
  283. return power
  284. def api_dyna(path, mode, ana):
  285. return classify_result(path, run(path, mode, ana), mode)
  286. if __name__ == '__main__':
  287. # path="./video/demo1.mp4"
  288. # api_dyna(path,"jump",0)
  289. # classify_result(path,run(path,"jump",0),"jump")
  290. # path="./video/juanfu.mp4"
  291. # api_dyna(path,"juanfu",0)
  292. # classify_result(path,run(path,"juanfu",0),"juanfu")
  293. # path = "./video/jump.mp4"
  294. # api_dyna(path, "jump", 0)
  295. # run(path, "jump", 0)
  296. # classify_result(path,run(path,"pingban",0),"pingban")
  297. # path="./video/gaotaitui.mp4"
  298. # api_dyna(path,"gaotaitui",0)
  299. # classify_result(path,run(path,"gaotaitui",0),"gaotaitui")
  300. # path="./video/shendun.mp4"
  301. # api_dyna(path,"shendun",0)
  302. '''
  303. os.path.split(path)
  304. tur=os.path.split(path)
  305. name=tur[len(tur)-1].split('.')[0]
  306. p=os.getcwd()
  307. q="./capture_image"+name
  308. qqq=os.path.abspath(q)
  309. #print(qqq)
  310. r="capture_image_result"+"48"+".png"
  311. a=os.path.join(qqq,r)
  312. '''