· 5 years ago · Jan 07, 2021, 10:42 AM
1# From Python
2# It requires OpenCV installed for Python
3import sys
4import cv2
5import os
6from sys import platform
7import argparse
8import time
9import math
10
11
12
13def pythag(x1, x2, y1, y2):
14 return math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
15
16def calc_hand_nose_distance(keypoints, keypoint_map):
17 nose_x = keypoints[0][keypoint_map['Nose']][0]
18 nose_y = keypoints[0][keypoint_map['Nose']][1]
19 eye_l_x = keypoints[0][keypoint_map['LEye']][0]
20 eye_l_y = keypoints[0][keypoint_map['LEye']][1]
21 eye_r_x = keypoints[0][keypoint_map['REye']][0]
22 eye_r_y = keypoints[0][keypoint_map['REye']][1]
23 wrist_l_x = keypoints[0][keypoint_map['LWrist']][0]
24 wrist_l_y = keypoints[0][keypoint_map['LWrist']][1]
25 wrist_r_x = keypoints[0][keypoint_map['RWrist']][0]
26 wrist_r_y = keypoints[0][keypoint_map['RWrist']][1]
27
28
29 eye_distance = pythag(eye_l_x, eye_r_x, eye_l_y, eye_r_y)
30 wrist_l_distance = pythag(nose_x, wrist_l_x, nose_y, wrist_l_y)
31 wrist_r_distance = pythag(nose_x, wrist_r_x, nose_y, wrist_r_y)
32
33 left_prox = wrist_l_distance/eye_distance
34 right_prox = wrist_r_distance/eye_distance
35
36 print("left_distance: ", left_prox)
37 print("right_distance: ", right_prox)
38
39 if left_prox < 3 or right_prox < 3:
40 return True
41 return False
42
43
44
45
46
47try:
48 # Import Openpose (Windows/Ubuntu/OSX)
49 dir_path = os.path.dirname(os.path.realpath(__file__))
50 try:
51 # Windows Import
52 if platform == "win32":
53 # Change these variables to point to the correct folder (Release/x64 etc.)
54 sys.path.append(dir_path + '/../../python/openpose/Release');
55 os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;'
56 import pyopenpose as op
57 else:
58 # Change these variables to point to the correct folder (Release/x64 etc.)
59 sys.path.append('../../python');
60 # If you run `make install` (default path is `/usr/local/python` for Ubuntu), you can also access the OpenPose/python module from there. This will install OpenPose and the python library at your desired installation path. Ensure that this is in your python path in order to use it.
61 # sys.path.append('/usr/local/python')
62 from openpose import pyopenpose as op
63 except ImportError as e:
64 print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?')
65 raise e
66
67 # Flags
68 parser = argparse.ArgumentParser()
69 parser.add_argument("--image_dir", default="../../../examples/media/", help="Process a directory of images. Read all standard formats (jpg, png, bmp, etc.).")
70 parser.add_argument("--no_display", default=False, help="Enable to disable the visual display.")
71 args = parser.parse_known_args()
72
73 # Custom Params (refer to include/openpose/flags.hpp for more parameters)
74 params = dict()
75 params["model_folder"] = "../../../models/"
76 params["num_gpu"] = 1
77 params["num_gpu_start"] = 2
78
79 # Add others in path?
80 for i in range(0, len(args[1])):
81 curr_item = args[1][i]
82 if i != len(args[1])-1: next_item = args[1][i+1]
83 else: next_item = "1"
84 if "--" in curr_item and "--" in next_item:
85 key = curr_item.replace('-','')
86 if key not in params: params[key] = "1"
87 elif "--" in curr_item and "--" not in next_item:
88 key = curr_item.replace('-','')
89 if key not in params: params[key] = next_item
90
91 # Construct it from system arguments
92 # op.init_argv(args[1])
93 # oppython = op.OpenposePython()
94
95 # Get keypoint mapping
96 poseModel = op.PoseModel.BODY_25
97 keypoint_map = {v:k for k,v in op.getPoseBodyPartMapping(poseModel).items()}
98
99 # Starting OpenPose
100 opWrapper = op.WrapperPython()
101 opWrapper.configure(params)
102 opWrapper.start()
103
104 # Read frames on directory
105 imagePaths = op.get_images_on_directory(args[0].image_dir);
106 start = time.time()
107
108 # Process and display images
109 file_suffix = 201
110 for imagePath in imagePaths:
111 datum = op.Datum()
112 imageToProcess = cv2.imread(imagePath)
113 datum.cvInputData = imageToProcess
114 opWrapper.emplaceAndPop(op.VectorDatum([datum]))
115
116 # print("Body keypoints: \n" + str(datum.poseKeypoints))
117
118 lwrist_x = datum.poseKeypoints[0][keypoint_map['LWrist']][0]
119 lwrist_y = datum.poseKeypoints[0][keypoint_map['LWrist']][1]
120 # print(nose_coor)
121
122 bad_touch = calc_hand_nose_distance(datum.poseKeypoints, keypoint_map)
123
124 if not args[0].no_display:
125 img = datum.cvOutputData
126 if(bad_touch):
127 cv2.putText(img,'STOP TOUCHING YOUR FACE!', (lwrist_x, lwrist_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
128 cv2.imshow("OpenPose 1.7.0 - Tutorial Python API", img)
129 cv2.imwrite('/Users/thefoolishpupil/Desktop/test_out/%04d.png' % (file_suffix), img)
130 key = cv2.waitKey(15)
131 if key == 27: break
132
133 file_suffix += 1
134
135
136 end = time.time()
137 print("OpenPose demo successfully finished. Total time: " + str(end - start) + " seconds")
138except Exception as e:
139 print(e)
140 sys.exit(-1)
141