daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Dec 16th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import json
  2. from pprint import pprint
  3. import cv2
  4. import numpy as np
  5. from scipy.spatial import distance
  6. import os
  7. import matplotlib.pyplot as plt
  8.  
  9. path_root_file = "D:\Work\ComputerV[261458]\Contest\Examples\\"
  10. path_out = "D:\Work\ComputerV[261458]\Contest\Frame_example\\"
  11. path_list_file = "D:\Work\ComputerV[261458]\Contest\list.txt"
  12. path_list_file_test = "D:\Work\ComputerV[261458]\Contest\\test.txt"
  13. path_root_json = "D:\Work\ComputerV[261458]\Contest\Json\\"
  14. path_pose_out = "D:\Work\ComputerV[261458]\Contest\Pose_out\\"
  15. path_save_plot = "D:\Work\ComputerV[261458]\Contest\Graph\\"
  16.  
  17. file_ans_r = open("D:\Work\ComputerV[261458]\Contest\\ans_r.txt","w")
  18. file_ans_l = open("D:\Work\ComputerV[261458]\Contest\\ans_l.txt","w")
  19. file_out = open("D:\Work\ComputerV[261458]\Contest\\result.txt",'w')
  20. data_window_size = 7
  21. l = []
  22. r = []
  23. def main():
  24.     try:
  25.         os.stat(path_out)
  26.     except:
  27.         os.mkdir(path_out)
  28.     try:
  29.         os.stat(path_root_json)
  30.     except:
  31.         os.mkdir(path_root_json)
  32.     # f = open(path_list_file_test,"r")
  33.     f = open(path_list_file,"r")
  34.     lines = f.readlines()
  35.     # for i in lines:
  36.     #     name = i.split(".")[0]
  37.     #     path_file = path_root_file + name + ".mp4"
  38.     #     print (i.split("\n")[0])
  39.     #     frame = sampling_frame(path_file)
  40.     #     c = 1
  41.     #     for i in frame:
  42.     #         if i.shape[0] > i.shape[1]:
  43.     #             i = cv2.resize(i, (360,640))
  44.     #         else:
  45.     #              i = cv2.resize(i, (640,360))
  46.     #         cv2.imwrite(path_out + name + "_" + str(c) + ".jpg",i)
  47.     #         c += 1
  48.     # script_generate()
  49.  
  50.     for line in lines:
  51.         ans_l = ans_r = 0
  52.         print (line.strip('\n'))
  53.         point_neck,point_l_shoulder_l,point_l_shoulder_r,point_r_shoulder_l,point_r_shoulder_r,point_l_elbow,point_r_elbow,point_l_wrist,point_r_wrist,conf_l,conf_r,size_l,size_r = read_json(path_root_json,line)
  54.        
  55.         left_top_arm_dist = cal_distance(point_l_shoulder_l,point_l_elbow)
  56.         right_top_arm_dist = cal_distance(point_r_shoulder_r,point_r_elbow)
  57.         left_bt_arm_dist = cal_distance(point_l_wrist,point_l_elbow)
  58.         right_bt_arm_dist = cal_distance(point_r_wrist,point_r_elbow)
  59.  
  60.         ratio_shoulder_l = cal_distance(point_l_shoulder_l,point_r_shoulder_l)
  61.         ratio_shoulder_r = cal_distance(point_l_shoulder_r,point_r_shoulder_r)
  62.         if size_l > 20:
  63.             punch_l_dist,max_l,min_l,del_conf_l = count_punch_distance(left_top_arm_dist,ratio_shoulder_l,left_bt_arm_dist,conf_l,line.strip('\n') + '_left_dist')
  64.             punch_l_conf = count_punch_conf(conf_l,del_conf_l,line.strip('\n') + '_left_conf')
  65.         if size_r > 20:
  66.             punch_r_dist,max_r,min_r,del_conf_r = count_punch_distance(right_top_arm_dist,ratio_shoulder_r,right_bt_arm_dist,conf_r,line.strip('\n') + '_right_dist')
  67.             punch_r_conf = count_punch_conf(conf_r,del_conf_r,line.strip('\n') + '_right_conf')
  68.         ans_l = punch_l_dist + punch_l_conf
  69.         ans_r = punch_r_dist + punch_r_conf
  70.         l.append(ans_l)
  71.         r.append(ans_r)
  72.         file_ans_l.write(str(ans_l) + "\n")
  73.         file_ans_r.write(str(ans_r) + "\n")
  74.  
  75.     for i in range(len(l)):
  76.         file_out.write(line.strip('\n') + ":" + str(l[i]) + " " + str(r[i]) + "\n")
  77.     print (ans_l,ans_r)
  78.  
  79.  
  80. def sampling_frame(path):
  81.     cap = cv2.VideoCapture(path)
  82.     video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
  83.     # print (video_length)
  84.     frames = []
  85.     frame_ids = []
  86.     if cap.isOpened() and video_length > 0:
  87.         if video_length >= 4:
  88.             for i in range(1,video_length):
  89.                 if video_length <= 150 :
  90.                     frame_ids.append(i)
  91.                 else:
  92.                     if i % 2 == 0:
  93.                         frame_ids.append(i)        
  94.         count = 0
  95.         success, image = cap.read()
  96.         while success:
  97.             if count in frame_ids:
  98.                 frames.append(image)
  99.             success, image = cap.read()
  100.             count += 1
  101.     return frames
  102.  
  103. def script_generate():
  104.     cmd = 'bin\OpenPoseDemo.exe --image_dir ' + path_out + ' -model_pose COCO -number_people_max 1 --write_json ' + path_root_json + ' --display 0 --render_pose 0'
  105.     # cmd = 'bin\OpenPoseDemo.exe --image_dir ' + path_out + ' -model_pose COCO -number_people_max 1 --write_json ' + path_root_json + ' --write_images ' + path_pose_out + ' --display 0'
  106.     # cmd = 'bin\OpenPoseDemo.exe --image_dir ' + path_out + ' -model_pose COCO -number_people_max 1 --write_images ' + path_pose_out + ' --display 0 '
  107.     os.system(cmd)
  108.  
  109. def read_json(path,line):
  110.     point_neck = []
  111.     point_r_shoulder_r = []
  112.     point_r_shoulder_l = []
  113.     point_l_shoulder_r = []
  114.     point_l_shoulder_l = []
  115.     point_r_elbow = []
  116.     point_l_elbow = []
  117.     point_r_wrist = []
  118.     point_l_wrist = []
  119.     conf_l = []
  120.     conf_r = []
  121.     n_frame = 1
  122.     size_l = 0
  123.     size_r = 0
  124.     for file in os.listdir(path):
  125.         l = file.split("_")
  126.         # print (l[0],line.strip('\n').strip('.mp4'))
  127.         if l[0] == line.split('.')[0]:
  128.             path_json = path + l[0] + '_' + str(n_frame) + '_keypoints.json'
  129.             # print (path_json)
  130.             is_exists = os.path.isfile(path_json)
  131.             if is_exists:
  132.                 with open(path_json) as f:
  133.                     data = json.load(f)
  134.                 # pprint(data)
  135.                 position = []
  136.                 confident = []
  137.                 count_3 = 1
  138.                 pair = []
  139.                 if data[u'people']:
  140.                     for i in data[u'people'][0][u'pose_keypoints_2d']:
  141.                         if count_3 % 3 == 0:
  142.                             position.append(pair)
  143.                             confident.append(i)
  144.                             pair = []
  145.                         elif count_3 % 3 == 1:
  146.                             pair.append(i)
  147.                         elif count_3 % 3 == 2:
  148.                             pair.append(i)
  149.                         count_3 += 1
  150.                     check_left = True
  151.                     check_right = True
  152.                     check_right_list = [1,2,3,5]
  153.                     check_left_list = [1,2,5,6]
  154.                     for i in check_left_list:
  155.                         if position[i][0] == 0:
  156.                             check_left = False
  157.                     for i in check_right_list:
  158.                         if position[i][0] == 0:
  159.                             check_right = False
  160.                     if check_right:
  161.                         size_r += 1
  162.                         point_neck.append(position[1])
  163.                         point_r_shoulder_r.append(position[2])
  164.                         point_r_elbow.append(position[3])
  165.                         point_l_shoulder_r.append(position[5])
  166.                         point_r_wrist.append(position[4])
  167.                         conf_r.append((confident[2] + confident[3])/2)
  168.                     if check_left:
  169.                         size_l += 1
  170.                         point_l_shoulder_l.append(position[5])
  171.                         point_r_shoulder_l.append(position[2])
  172.                         point_l_elbow.append(position[6])
  173.                         point_l_wrist.append(position[7])
  174.                         conf_l.append((confident[5] + confident[6])/2)
  175.             else :
  176.                 n_frame = 0
  177.         n_frame += 1
  178.     return point_neck,point_l_shoulder_l,point_l_shoulder_r,point_r_shoulder_l,point_r_shoulder_r,point_l_elbow,point_r_elbow,point_l_wrist,point_r_wrist,conf_l,conf_r,size_l,size_r
  179.  
  180. def find_bd_box(point_1,point_2):
  181.     min_x = 50000
  182.     min_y = 50000
  183.     max_x = -1
  184.     max_y = -1
  185.     for i in point_1:
  186.         min_x = min(min_x,i[0])
  187.         min_y = min(min_y,i[1])
  188.         max_x = max(max_x,i[0])
  189.         max_y = max(max_y,i[1])
  190.     for i in point_2:
  191.         min_x = min(min_x,i[0])
  192.         min_y = min(min_y,i[1])
  193.         max_x = max(max_x,i[0])
  194.         max_y = max(max_y,i[1])
  195.     return min_x,min_y,max_x-min_x,max_y-min_y
  196.  
  197. def cal_distance(point_list_1,point_list_2):
  198.     dist_list = []
  199.     for i in range(len(point_list_1)):
  200.         if point_list_1[i] == [0,0] or point_list_2[i] == [0,0]:
  201.             dist_list.append(0)
  202.         else :
  203.             dist_list.append(distance.euclidean(point_list_1[i],point_list_2[i]))
  204.     return dist_list
  205.  
  206. def data_process (data,window_filter_size):
  207.     data_filter = filter_data(data,window_filter_size,1)
  208.     max_data,min_data,avg_data,per_avg_data = math_process_data(data_filter,window_filter_size)
  209.     return data_filter,max_data,min_data,avg_data,per_avg_data
  210.  
  211. def filter_data(data,window_filter_size,scale_rate):
  212.     data_filtered = []
  213.     window = []
  214.     for i in range(window_filter_size):
  215.         if i < window_filter_size/2:
  216.             window.append(0)
  217.         else:
  218.             window.append(data[i])
  219.     for i in range(len(data)):
  220.         s = sum(window) + (window[int(window_filter_size/2)+1]*scale_rate) #increase mean Doble mid
  221.         data_filtered.append((s/window_filter_size)/2)
  222.  
  223.         for j in range(window_filter_size):
  224.             if j+1 < window_filter_size:
  225.                 window[j] = window[j+1]
  226.             else:
  227.                 if i+int(window_filter_size/2)+1 < len(data):
  228.                     window[j] = data[i+int(window_filter_size/2)+1]
  229.                 else:
  230.                     window[j] = 0
  231.     return data_filtered
  232.  
  233. def math_process_data (data_filter,window_filter_size):
  234.     cut_zero_data = []
  235.     for i in range(len(data_filter)):
  236.         if int(window_filter_size/2) < i < len(data_filter) - int(window_filter_size/2):
  237.             cut_zero_data.append(data_filter[i])
  238.     max_data = max(cut_zero_data)
  239.     min_data = min(cut_zero_data)
  240.     avg_data = sum(cut_zero_data)/len(cut_zero_data)
  241.     per_avg_data = (avg_data - min_data)/(max_data - min_data)*100
  242.     return max_data,min_data,avg_data,per_avg_data
  243.  
  244. def count_punch_distance(top_arm_distance,ratio_shoulder,bt_arm_distance,conf,label):
  245.     upper_rate_shoulder_top = []
  246.     lower_rate_shoulder_top = []
  247.     del_conf = []
  248.     top_arm_dist_filtered,max_data,min_data,avg_data,per_avg_data = data_process(top_arm_distance,data_window_size)
  249.     bt_arm_dist_filtered = filter_data(bt_arm_distance,data_window_size,0)
  250.     for i in range(len(ratio_shoulder)):
  251.         if ratio_shoulder[i] != 0:
  252.             upper_rate_shoulder_top.append(ratio_shoulder[i]*0.80)
  253.             lower_rate_shoulder_top.append(ratio_shoulder[i]*0.74)
  254.     upper_rate_shoulder_top = filter_data(upper_rate_shoulder_top,15,1)
  255.     lower_rate_shoulder_top = filter_data(lower_rate_shoulder_top,15,1)
  256.     # plt.xlabel(label)
  257.     # plt.ylim(0, 120)
  258.     # plt.plot(np.arange(len(top_arm_dist_filtered)), top_arm_dist_filtered, color="red")
  259.     # plt.plot(np.arange(len(upper_rate_shoulder_top)), upper_rate_shoulder_top, color="black")
  260.     # plt.plot(np.arange(len(lower_rate_shoulder_top)), lower_rate_shoulder_top, color="gray")
  261.     # plt.savefig(path_save_plot + label + '.png')
  262.     # plt.close()
  263.     if len(top_arm_dist_filtered) != 0:
  264.         if per_avg_data > 40:
  265.             if max_data - min_data > 10:
  266.                 punch = 0
  267.                 punching = True
  268.                 index = 0
  269.                 for i in range(len(top_arm_dist_filtered)):
  270.                     if top_arm_dist_filtered[i] > upper_rate_shoulder_top[i]:
  271.                         punching = False
  272.                     if punching:
  273.                         del_conf.append(i)
  274.                     if top_arm_dist_filtered[i] < lower_rate_shoulder_top[i] and not punching and conf[i] > 0.2:
  275.                         punch += 1
  276.                         punching = True
  277.                         del_conf.append(i)
  278.                 return punch,max_data,min_data,del_conf
  279.             return 0,max_data,min_data,del_conf
  280.         else:
  281.             return 0,max_data,min_data,del_conf
  282.     else :
  283.         # print("D")
  284.         return 0,0,0,del_conf
  285.  
  286. def count_punch_conf(conf,del_conf,label):
  287.     for i in del_conf:
  288.         conf.pop(i)
  289.         for j in range(len(del_conf)):
  290.             if del_conf[j] > i :
  291.                 del_conf[j] -= 1
  292.  
  293.     conf_filtered = filter_data(conf,3,0)
  294.     upper_ratio = ((max(conf_filtered) - min(conf_filtered))*0.79) + min(conf_filtered)
  295.     lower_ratio = ((max(conf_filtered) - min(conf_filtered))*0.38)  + min(conf_filtered)
  296.     x = []
  297.     y = []
  298.     for i in range(len(conf_filtered)):
  299.         x.append(upper_ratio)
  300.         y.append(lower_ratio)
  301.     # plt.ylim(0, 1)
  302.     # plt.xlabel(label)
  303.     # plt.plot(np.arange(len(conf_filtered)),conf_filtered, color="green")
  304.     # plt.plot(np.arange(len(conf_filtered)),x, color="black")
  305.     # plt.plot(np.arange(len(conf_filtered)),y, color="gray")
  306.     # plt.show()
  307.     # plt.savefig(path_save_plot + label + '.png')
  308.     # print (max(conf) - min(conf))
  309.     if max(conf) - min(conf) > 0.1:
  310.         punching = True
  311.         punch = 0
  312.         for i in range(1,len(conf_filtered)-1):
  313.             # print (conf_filtered[i],lower_ratio)
  314.             if conf_filtered[i] < lower_ratio and not punching:
  315.                 punch += 1
  316.                 punching = True
  317.             if conf_filtered[i] > upper_ratio:
  318.                 punching = False
  319.         return punch
  320.     return 0
  321.  
  322. main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top