From 164b6ca6ae73963c919012b1a9948c650a50a77d Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 9 Aug 2020 14:45:36 +0000 Subject: [PATCH 1/6] Restyled by autopep8 --- gui_cvs/hello.py | 5 +- gui_cvs/table.py | 11 +- gui_wizard/helloworld.py | 53 +++++----- src/cvs/webcam.py | 34 +++--- src/facencnn/facepose.py | 68 ++++++------ src/handtf/utils/detector_utils.py | 35 +++--- src/handtf/webcam.py | 34 +++--- src/mutilpose/posenet/decode_multi.py | 12 ++- src/mutilpose/posenet/model.py | 3 +- src/mutilpose/posenet/utils.py | 37 ++++--- src/mutilpose/webcam_demo.py | 63 +++++------ src/singlepose/webpose.py | 146 +++++++++++++------------- 12 files changed, 261 insertions(+), 240 deletions(-) diff --git a/gui_cvs/hello.py b/gui_cvs/hello.py index 3c22f63..13f1924 100644 --- a/gui_cvs/hello.py +++ b/gui_cvs/hello.py @@ -1,4 +1,6 @@ from cvs import * + + class MyApp(App): def __init__(self, *args): super(MyApp, self).__init__(*args) @@ -7,5 +9,6 @@ def main(self): # return of the root widget return gui.Label("Hello world!", width=100, height=30) + initcv(cvs.openwin) -startcv(MyApp) \ No newline at end of file +startcv(MyApp) diff --git a/gui_cvs/table.py b/gui_cvs/table.py index 1dd4279..477ac7e 100755 --- a/gui_cvs/table.py +++ b/gui_cvs/table.py @@ -1,10 +1,13 @@ from cvs import * + + class MyApp(App): def __init__(self, *args): super(MyApp, self).__init__(*args) def main(self): - wid = gui.VBox(width=500, height=500, style={'margin':'5px auto', 'padding': '10px'}) + wid = gui.VBox(width=500, height=500, style={ + 'margin': '5px auto', 'padding': '10px'}) lbl_description = gui.Label("""Example about TableWidget usage. Change rows and columns count in order to see the behaviour. @@ -56,17 +59,17 @@ def on_column_count_change(self, emitter, value, table): def fill_table(self, emitter, table): for ri in range(table.row_count()): for ci in range(table.column_count()): - table.item_at(ri, ci).set_text("row:%s,column:%s"%(str(ri),str(ci))) + table.item_at(ri, ci).set_text( + "row:%s,column:%s" % (str(ri), str(ci))) def on_use_title_change(self, emitter, value, table): value = value == 'true' table.set_use_title(value) def on_table_item_changed(self, table, item, new_value, row, column): - print("text: %s row: %s column: %s"%(new_value, row, column)) + print("text: %s row: %s column: %s" % (new_value, row, column)) if __name__ == "__main__": initcv(cvs.openwin) startcv(MyApp) - diff --git a/gui_wizard/helloworld.py b/gui_wizard/helloworld.py index 77e35b3..72f0ae7 100644 --- a/gui_wizard/helloworld.py +++ b/gui_wizard/helloworld.py @@ -10,60 +10,63 @@ class untitled(App): def __init__(self, *args, **kwargs): - #DON'T MAKE CHANGES HERE, THIS METHOD GETS OVERWRITTEN WHEN SAVING IN THE EDITOR + # DON'T MAKE CHANGES HERE, THIS METHOD GETS OVERWRITTEN WHEN SAVING IN THE EDITOR if 'editing_mode' not in kwargs.keys(): - super(untitled, self).__init__(*args, static_file_path={'my_res':'./res/'}) + super(untitled, self).__init__( + *args, static_file_path={'my_res': './res/'}) def idle(self): - #idle function called every update cycle + # idle function called every update cycle pass - + def main(self): return untitled.construct_ui(self) - + @staticmethod def construct_ui(self): - #DON'T MAKE CHANGES HERE, THIS METHOD GETS OVERWRITTEN WHEN SAVING IN THE EDITOR + # DON'T MAKE CHANGES HERE, THIS METHOD GETS OVERWRITTEN WHEN SAVING IN THE EDITOR mainContainer = Widget() - mainContainer.attributes.update({"editor_baseclass":"Widget","editor_tag_type":"widget","editor_newclass":"False","editor_constructor":"()","class":"Widget","editor_varname":"mainContainer"}) - mainContainer.style.update({"width":"266px","position":"absolute","top":"61px","left":"16px","margin":"0px","overflow":"auto","height":"257px"}) + mainContainer.attributes.update({"editor_baseclass": "Widget", "editor_tag_type": "widget", "editor_newclass": "False", + "editor_constructor": "()", "class": "Widget", "editor_varname": "mainContainer"}) + mainContainer.style.update({"width": "266px", "position": "absolute", "top": "61px", + "left": "16px", "margin": "0px", "overflow": "auto", "height": "257px"}) button = Button('Say Hello') - button.attributes.update({"editor_baseclass":"Button","editor_tag_type":"widget","editor_newclass":"False","editor_constructor":"('Say Hello')","class":"Button","editor_varname":"button"}) - button.style.update({"width":"149px","position":"absolute","top":"80px","left":"37px","margin":"0px","overflow":"auto","height":"29px"}) - mainContainer.append(button,'button') + button.attributes.update({"editor_baseclass": "Button", "editor_tag_type": "widget", "editor_newclass": "False", + "editor_constructor": "('Say Hello')", "class": "Button", "editor_varname": "button"}) + button.style.update({"width": "149px", "position": "absolute", "top": "80px", + "left": "37px", "margin": "0px", "overflow": "auto", "height": "29px"}) + mainContainer.append(button, 'button') label = Label('My label') - label.attributes.update({"editor_baseclass":"Label","editor_tag_type":"widget","editor_newclass":"False","editor_constructor":"('My label')","class":"Label","editor_varname":"label"}) - label.style.update({"width":"98px","position":"absolute","top":"20px","left":"20px","margin":"0px","overflow":"auto","height":"25px"}) - mainContainer.append(label,'label') + label.attributes.update({"editor_baseclass": "Label", "editor_tag_type": "widget", "editor_newclass": "False", + "editor_constructor": "('My label')", "class": "Label", "editor_varname": "label"}) + label.style.update({"width": "98px", "position": "absolute", "top": "20px", + "left": "20px", "margin": "0px", "overflow": "auto", "height": "25px"}) + mainContainer.append(label, 'label') mainContainer.children['button'].onclick.do(self.onclick_button) - self.mainContainer = mainContainer return self.mainContainer - + def onclick_button(self, emitter): self.mainContainer.children['label'].set_text('hello world') - - - -#processing_code +# processing_code def process(): - cap=cvs.VideoCapture(1) + cap = cvs.VideoCapture(1) while True: sleep(30) - img =cap.read() + img = cap.read() - if img is None : + if img is None: continue cvs.imshow(img) + if __name__ == "__main__": - + initcv(cvs.openwin) startcv(untitled) - diff --git a/src/cvs/webcam.py b/src/cvs/webcam.py index be7b29c..c0c5960 100644 --- a/src/cvs/webcam.py +++ b/src/cvs/webcam.py @@ -6,45 +6,47 @@ class untitled(App): def __init__(self, *args, **kwargs): - #DON'T MAKE CHANGES HERE, THIS METHOD GETS OVERWRITTEN WHEN SAVING IN THE EDITOR + # DON'T MAKE CHANGES HERE, THIS METHOD GETS OVERWRITTEN WHEN SAVING IN THE EDITOR if 'editing_mode' not in kwargs.keys(): - super(untitled, self).__init__(*args, static_file_path={'my_res':'./res/'}) + super(untitled, self).__init__( + *args, static_file_path={'my_res': './res/'}) def idle(self): - #idle function called every update cycle + # idle function called every update cycle pass - + def main(self): return untitled.construct_ui(self) - + @staticmethod def construct_ui(self): - #DON'T MAKE CHANGES HERE, THIS METHOD GETS OVERWRITTEN WHEN SAVING IN THE EDITOR + # DON'T MAKE CHANGES HERE, THIS METHOD GETS OVERWRITTEN WHEN SAVING IN THE EDITOR aidcam = OpencvVideoWidget(self) - aidcam.set_identifier("myimage_receiver")#aidcam.attributes.update({"src":"/528175722896/get_image_data","editor_newclass":"False","editor_baseclass":"OpencvVideoWidget","editor_constructor":"()","class":"OpencvVideoWidget","editor_tag_type":"widget","editor_varname":"aidcam"}) - aidcam.style.update({"width":"190px","position":"absolute","top":"20px","left":"20px","margin":"0px","overflow":"auto","height":"177px"}) - + # aidcam.attributes.update({"src":"/528175722896/get_image_data","editor_newclass":"False","editor_baseclass":"OpencvVideoWidget","editor_constructor":"()","class":"OpencvVideoWidget","editor_tag_type":"widget","editor_varname":"aidcam"}) + aidcam.set_identifier("myimage_receiver") + aidcam.style.update({"width": "190px", "position": "absolute", "top": "20px", + "left": "20px", "margin": "0px", "overflow": "auto", "height": "177px"}) self.aidcam = aidcam return self.aidcam - -#processing_code + +# processing_code def process(): - cap=cvs.VideoCapture(1) + cap = cvs.VideoCapture(1) while True: sleep(30) - img =cap.read() + img = cap.read() - if img is None : + if img is None: continue cvs.imshow(img) + if __name__ == "__main__": - + initcv(process) startcv(untitled) - \ No newline at end of file diff --git a/src/facencnn/facepose.py b/src/facencnn/facepose.py index 2e47b22..67ce9b7 100644 --- a/src/facencnn/facepose.py +++ b/src/facencnn/facepose.py @@ -4,56 +4,56 @@ def main(): - camid=1 - cap=cvs.VideoCapture(camid) + camid = 1 + cap = cvs.VideoCapture(camid) facerecog = facerecognition.FaceRecognition("./models", 0.63) - fcount=0 + fcount = 0 start = time.time() while True: sleep(30) - img =cvs.read() + img = cvs.read() - if img is None : + if img is None: continue fcount += 1 # global lbs - lbs = 'Average FPS: '+ str(fcount / (time.time() - start)) + lbs = 'Average FPS: ' + str(fcount / (time.time() - start)) cvs.setLbs(lbs) - if camid==1: - img=cv2.flip(img,1) + if camid == 1: + img = cv2.flip(img, 1) - #img=cv2.resize(img,(112,112)) + # img=cv2.resize(img,(112,112)) image_char = img.astype(np.uint8).tostring() rets = facerecog.getfacepose(img.shape[0], img.shape[1], image_char) - #print 'rets:',rets + # print 'rets:',rets for ret in rets: - #for ret in each: - print ('draw bounding box for the face') + # for ret in each: + print('draw bounding box for the face') #cvs.infoshow('draw bounding box for the face') rect = ret['rect'] - #print rect + # print rect mtcnn = ret['mtcnn'] - #print mtcnn + # print mtcnn for i in range(5): - cvs.circle(img,(mtcnn[i],mtcnn[5+i]),2,(0,0,255),2) - keypoint=ret['keypoints'] - #print keypoint + cvs.circle(img, (mtcnn[i], mtcnn[5+i]), 2, (0, 0, 255), 2) + keypoint = ret['keypoints'] + # print keypoint p1 = (int(rect[0]), int(rect[1])) p2 = (int(rect[0]+rect[2]), int(rect[1]+rect[3])) #draw_name(img, rect, ret['name']) - cvs.rectangle(img, p1,p2, (0, 255, 0) , 3, 1) + cvs.rectangle(img, p1, p2, (0, 255, 0), 3, 1) for p in range(106): - #print p*2,' = ',keypoint[p*2] - #print p*2+1,' = ',keypoint[p*2+1] - k1=int(rect[0]+keypoint[p*2]) - k2=int(rect[1]+keypoint[p*2+1]) - cv2.circle(img,(k1,k2),2,(253,0,0),2) + # print p*2,' = ',keypoint[p*2] + # print p*2+1,' = ',keypoint[p*2+1] + k1 = int(rect[0]+keypoint[p*2]) + k2 = int(rect[1]+keypoint[p*2+1]) + cv2.circle(img, (k1, k2), 2, (253, 0, 0), 2) cvs.imshow(img) @@ -61,29 +61,29 @@ def main(): class MyApp(App): def __init__(self, *args): super(MyApp, self).__init__(*args) - + def idle(self): - #idle function called every update cycle + # idle function called every update cycle self.lbl.set_text(cvs.getLbs()) - + def main(self): - #creating a container VBox type, vertical (you can use also HBox or Widget) - main_container = gui.VBox(width=360, height=680, style={'margin':'0px auto'}) - + # creating a container VBox type, vertical (you can use also HBox or Widget) + main_container = gui.VBox(width=360, height=680, style={ + 'margin': '0px auto'}) + self.aidcam = OpencvVideoWidget(self, width=340, height=480) self.aidcam.style['margin'] = '10px' - + self.aidcam.set_identifier("myimage_receiver") main_container.append(self.aidcam) - self.lbl = gui.Label('This show fps!', width=360, height=30, margin='50px',) + self.lbl = gui.Label('This show fps!', width=360, + height=30, margin='50px',) main_container.append(self.lbl) return main_container - + if __name__ == '__main__': initcv(main) startcv(MyApp) - - diff --git a/src/handtf/utils/detector_utils.py b/src/handtf/utils/detector_utils.py index 6b546e6..7f176ab 100644 --- a/src/handtf/utils/detector_utils.py +++ b/src/handtf/utils/detector_utils.py @@ -38,13 +38,13 @@ def load_inference_graph(): serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') - + config = tf.ConfigProto(device_count={"CPU": 4}, - inter_op_parallelism_threads = 1, - intra_op_parallelism_threads = 4, - log_device_placement=True) + inter_op_parallelism_threads=1, + intra_op_parallelism_threads=4, + log_device_placement=True) - sess = tf.Session(config=config,graph=detection_graph) + sess = tf.Session(config=config, graph=detection_graph) print("> ====== Inference graph loaded.") return detection_graph, sess @@ -58,14 +58,15 @@ def draw_box_on_image(num_hands_detect, score_thresh, scores, boxes, classes, im avg_width = 4.0 # To more easily differetiate distances and detected bboxes color = None - color0 = (255,0,0) - color1 = (0,50,255) + color0 = (255, 0, 0) + color1 = (0, 50, 255) for i in range(num_hands_detect): if (scores[i] > score_thresh): - if classes[i] == 1: id = 'open' + if classes[i] == 1: + id = 'open' if classes[i] == 2: - id ='closed' - avg_width = 3.0 # To compensate bbox size change + id = 'closed' + avg_width = 3.0 # To compensate bbox size change color = color0 if i == 0 else color1 (left, right, top, bottom) = (boxes[i][1] * im_width, boxes[i][3] * im_width, @@ -75,17 +76,17 @@ def draw_box_on_image(num_hands_detect, score_thresh, scores, boxes, classes, im dist = distance_to_camera(avg_width, focalLength, int(right-left)) - cv2.rectangle(image_np, p1, p2, color , 3, 1) + cv2.rectangle(image_np, p1, p2, color, 3, 1) cv2.putText(image_np, 'hand '+str(i)+': '+id, (int(left), int(top)-5), - cv2.FONT_HERSHEY_SIMPLEX, 0.5 , color, 2) + cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) cv2.putText(image_np, 'confidence: '+str("{0:.2f}".format(scores[i])), - (int(left),int(top)-20), - cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2) + (int(left), int(top)-20), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) cv2.putText(image_np, 'distance: '+str("{0:.2f}".format(dist)+' inches'), - (int(im_width*0.7),int(im_height*0.9+30*i)), + (int(im_width*0.7), int(im_height*0.9+30*i)), cv2.FONT_HERSHEY_SIMPLEX, 0.75, color, 2) @@ -94,10 +95,14 @@ def draw_text_on_image(fps, image_np): cv2.putText(image_np, fps, (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (77, 255, 9), 2) # compute and return the distance from the hand to the camera using triangle similarity + + def distance_to_camera(knownWidth, focalLength, pixelWidth): return (knownWidth * focalLength) / pixelWidth # Actual detection .. generate scores and bounding boxes given an image + + def detect_objects(image_np, detection_graph, sess): # Definite input and output Tensors for detection_graph image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') diff --git a/src/handtf/webcam.py b/src/handtf/webcam.py index b6509c1..ab5ee6e 100644 --- a/src/handtf/webcam.py +++ b/src/handtf/webcam.py @@ -7,7 +7,7 @@ ap = argparse.ArgumentParser() ap.add_argument('-d', '--display', dest='display', type=int, - default=1, help='Display the detected images using OpenCV. This reduces FPS') + default=1, help='Display the detected images using OpenCV. This reduces FPS') args = vars(ap.parse_args()) @@ -18,7 +18,7 @@ def main(): # Detection confidence threshold to draw bounding box score_thresh = 0.60 - vs=cvs.VideoCapture(1) + vs = cvs.VideoCapture(1) # max number of hands we want to detect/track num_hands_detect = 1 @@ -33,7 +33,7 @@ def main(): while True: sleep(30) # Read Frame and process - frame =cvs.read() + frame = cvs.read() if frame is None: continue frame = cv2.resize(frame, (640, 480)) @@ -51,7 +51,6 @@ def main(): boxes, scores, classes = detector_utils.detect_objects( frame, detection_graph, sess) - # Draw bounding boxeses and text detector_utils.draw_box_on_image( num_hands_detect, score_thresh, scores, boxes, classes, im_width, im_height, frame) @@ -68,46 +67,45 @@ def main(): if args['display']: - detector_utils.draw_text_on_image("FPS : " + str("{0:.2f}".format(fps)), frame) + detector_utils.draw_text_on_image( + "FPS : " + str("{0:.2f}".format(fps)), frame) cvs.imshow(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)) - print("Average FPS: ", str("{0:.2f}".format(fps))) - - except KeyboardInterrupt: print("Average FPS: ", str("{0:.2f}".format(fps))) - + class MyApp(App): def __init__(self, *args): super(MyApp, self).__init__(*args) def idle(self): - #idle function called every update cycle + # idle function called every update cycle self.lbl.set_text(cvs.getLbs()) - + def main(self): # initcv(process) - #creating a container VBox type, vertical (you can use also HBox or Widget) - main_container = gui.VBox(width=360, height=680, style={'margin':'0px auto'}) - + # creating a container VBox type, vertical (you can use also HBox or Widget) + main_container = gui.VBox(width=360, height=680, style={ + 'margin': '0px auto'}) + self.aidcam = OpencvVideoWidget(self, width=340, height=480) self.aidcam.style['margin'] = '10px' self.aidcam.set_identifier("myimage_receiver") main_container.append(self.aidcam) - + # Display FPS on frame - self.lbl = gui.Label('This is a LABEL!', width=360, height=30, margin='10px') + self.lbl = gui.Label('This is a LABEL!', width=360, + height=30, margin='10px') main_container.append(self.lbl) return main_container - - + if __name__ == '__main__': initcv(main) startcv(MyApp) diff --git a/src/mutilpose/posenet/decode_multi.py b/src/mutilpose/posenet/decode_multi.py index 87a92da..b6d7fbb 100644 --- a/src/mutilpose/posenet/decode_multi.py +++ b/src/mutilpose/posenet/decode_multi.py @@ -35,7 +35,8 @@ def get_instance_score_fast( keypoint_scores, keypoint_coords): if exist_pose_coords.shape[0]: - s = np.sum((exist_pose_coords - keypoint_coords) ** 2, axis=2) > squared_nms_radius + s = np.sum((exist_pose_coords - keypoint_coords) + ** 2, axis=2) > squared_nms_radius not_overlapped_scores = np.sum(keypoint_scores[np.all(s, axis=0)]) else: not_overlapped_scores = np.sum(keypoint_scores) @@ -112,15 +113,18 @@ def decode_multiple_poses( squared_nms_radius = nms_radius ** 2 - scored_parts = build_part_with_score_fast(score_threshold, LOCAL_MAXIMUM_RADIUS, scores) + scored_parts = build_part_with_score_fast( + score_threshold, LOCAL_MAXIMUM_RADIUS, scores) scored_parts = sorted(scored_parts, key=lambda x: x[0], reverse=True) # change dimensions from (h, w, x) to (h, w, x//2, 2) to allow return of complete coord array height = scores.shape[0] width = scores.shape[1] offsets = offsets.reshape(height, width, 2, -1).swapaxes(2, 3) - displacements_fwd = displacements_fwd.reshape(height, width, 2, -1).swapaxes(2, 3) - displacements_bwd = displacements_bwd.reshape(height, width, 2, -1).swapaxes(2, 3) + displacements_fwd = displacements_fwd.reshape( + height, width, 2, -1).swapaxes(2, 3) + displacements_bwd = displacements_bwd.reshape( + height, width, 2, -1).swapaxes(2, 3) for root_score, root_id, root_coord in scored_parts: root_image_coords = root_coord * output_stride + offsets[ diff --git a/src/mutilpose/posenet/model.py b/src/mutilpose/posenet/model.py index da992a4..d650ce4 100644 --- a/src/mutilpose/posenet/model.py +++ b/src/mutilpose/posenet/model.py @@ -34,7 +34,8 @@ def load_config(model_ord): def load_model(model_id, sess, model_dir=MODEL_DIR): model_ord = model_id_to_ord(model_id) model_cfg = load_config(model_ord) - model_path = os.path.join(model_dir, 'model-%s.pb' % model_cfg['checkpoint_name']) + model_path = os.path.join(model_dir, 'model-%s.pb' % + model_cfg['checkpoint_name']) if not os.path.exists(model_path): print('Cannot find model file %s, converting from tfjs...' % model_path) from posenet.converter.tfjs2python import convert diff --git a/src/mutilpose/posenet/utils.py b/src/mutilpose/posenet/utils.py index fdc6631..8052d5a 100644 --- a/src/mutilpose/posenet/utils.py +++ b/src/mutilpose/posenet/utils.py @@ -3,9 +3,10 @@ import posenet.constants import sys -scale_factor=1.0 +scale_factor = 1.0 if sys.version_info < (3, 5): - scale_factor=0.7125 + scale_factor = 0.7125 + def valid_resolution(width, height, output_stride=16): target_width = (int(width) // output_stride) * output_stride + 1 @@ -16,9 +17,11 @@ def valid_resolution(width, height, output_stride=16): def _process_input(source_img, scale_factor=1.0, output_stride=16): target_width, target_height = valid_resolution( source_img.shape[1] * scale_factor, source_img.shape[0] * scale_factor, output_stride=output_stride) - scale = np.array([source_img.shape[0] / target_height, source_img.shape[1] / target_width]) + scale = np.array([source_img.shape[0] / target_height, + source_img.shape[1] / target_width]) - input_img = cv2.resize(source_img, (target_width, target_height), interpolation=cv2.INTER_LINEAR) + input_img = cv2.resize( + source_img, (target_width, target_height), interpolation=cv2.INTER_LINEAR) input_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB).astype(np.float32) input_img = input_img * (2.0 / 255.0) - 1.0 input_img = input_img.reshape(1, target_height, target_width, 3) @@ -27,7 +30,7 @@ def _process_input(source_img, scale_factor=1.0, output_stride=16): def read_cap(img, scale_factor=1.0, output_stride=16): #res, img = cap.read() - #if not res: + # if not res: # raise IOError("webcam failure") return _process_input(img, scale_factor, output_stride) @@ -57,7 +60,8 @@ def get_adjacent_keypoints(keypoint_scores, keypoint_coords, min_confidence=0.1) if keypoint_scores[left] < min_confidence or keypoint_scores[right] < min_confidence: continue results.append( - np.array([keypoint_coords[left][::-1]/scale_factor, keypoint_coords[right][::-1]/scale_factor]).astype(np.int32), + np.array([keypoint_coords[left][::-1]/scale_factor, + keypoint_coords[right][::-1]/scale_factor]).astype(np.int32), ) return results @@ -73,7 +77,8 @@ def draw_skeleton( new_keypoints = get_adjacent_keypoints( keypoint_scores[ii, :], keypoint_coords[ii, :, :], min_part_confidence) adjacent_keypoints.extend(new_keypoints) - out_img = cv2.polylines(out_img, adjacent_keypoints, isClosed=False, color=(255, 255, 0)) + out_img = cv2.polylines(out_img, adjacent_keypoints, + isClosed=False, color=(255, 255, 0)) return out_img @@ -94,18 +99,20 @@ def draw_skel_and_kp( for ks, kc in zip(keypoint_scores[ii, :], keypoint_coords[ii, :, :]): if ks < min_part_score: continue - cv_keypoints.append(cv2.KeyPoint(kc[1]/scale_factor, kc[0]/scale_factor, 10. * ks)) + cv_keypoints.append(cv2.KeyPoint( + kc[1]/scale_factor, kc[0]/scale_factor, 10. * ks)) - #out_img = cv2.drawKeypoints( + # out_img = cv2.drawKeypoints( # out_img, cv_keypoints, np.array([]), (0, 0, 255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) - #return out_img - out_img2 = cv2.polylines(out_img, adjacent_keypoints, False, (255, 255, 0),2) - #out_img1 = cv2.drawKeypoints( + # return out_img + out_img2 = cv2.polylines( + out_img, adjacent_keypoints, False, (255, 255, 0), 2) + # out_img1 = cv2.drawKeypoints( # out_img, cv_keypoints, np.array([]), (0, 0, 255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) - if out_img2 is None : + if out_img2 is None: out_img1 = cv2.drawKeypoints( - out_img, cv_keypoints, np.array([]), (0, 0, 255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) + out_img, cv_keypoints, np.array([]), (0, 0, 255), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) return out_img1 out_img1 = cv2.drawKeypoints( - out_img2, cv_keypoints, np.array([]), (0, 0, 255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) + out_img2, cv_keypoints, np.array([]), (0, 0, 255), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) return out_img1 diff --git a/src/mutilpose/webcam_demo.py b/src/mutilpose/webcam_demo.py index 213be37..9e10754 100644 --- a/src/mutilpose/webcam_demo.py +++ b/src/mutilpose/webcam_demo.py @@ -1,14 +1,11 @@ -from cvs import * - -print ('import tensorflow...wait...') -import tensorflow as tf -import time -import argparse - import posenet +import argparse +import time +import tensorflow as tf +from cvs import * - +print('import tensorflow...wait...') parser = argparse.ArgumentParser() @@ -16,36 +13,32 @@ parser.add_argument('--cam', type=int, default=0) parser.add_argument('--cam_width', type=int, default=640) parser.add_argument('--cam_height', type=int, default=480) -parser.add_argument('--scale_factor', type=float, default=0.7125)#0.7125 +parser.add_argument('--scale_factor', type=float, default=0.7125) # 0.7125 args = parser.parse_args() -cam_id=args.cam +cam_id = args.cam def main(): - - cap=cvs.VideoCapture(0) - + cap = cvs.VideoCapture(0) with tf.Session() as sess: - print ('load models...') + print('load models...') model_cfg, model_outputs = posenet.load_model(args.model, sess) output_stride = model_cfg['output_stride'] - start = time.time() frame_count = 0 while True: sleep(30) - img =cvs.read() - frame_count+=1 - if img is None : + img = cvs.read() + frame_count += 1 + if img is None: continue - if cam_id >0: + if cam_id > 0: img = cvs.flip(img, 0) - input_image, display_image, output_scale = posenet.read_cap( img, scale_factor=args.scale_factor, output_stride=output_stride) @@ -64,48 +57,48 @@ def main(): min_pose_score=0.15) keypoint_coords *= output_scale - #print keypoint_coords + # print keypoint_coords # TODO this isn't particularly fast, use GL for drawing and display someday... overlay_image = posenet.draw_skel_and_kp( display_image, pose_scores, keypoint_scores, keypoint_coords, min_pose_score=0.15, min_part_score=0.1) - cvs.imshow(overlay_image) frame_count += 1 # global lbs - lbs = 'Average FPS: '+ str(frame_count / (time.time() - start)) + lbs = 'Average FPS: ' + str(frame_count / (time.time() - start)) cvs.setLbs(lbs) - + class MyApp(App): def __init__(self, *args): super(MyApp, self).__init__(*args) - + def idle(self): - #idle function called every update cycle,we show the FPS + # idle function called every update cycle,we show the FPS self.lbl.set_text(cvs.getLbs()) - + def main(self): - #creating a container VBox type, vertical (you can use also HBox or Widget) - main_container = gui.VBox(width=360, height=680, style={'margin':'0px auto'}) - + # creating a container VBox type, vertical (you can use also HBox or Widget) + main_container = gui.VBox(width=360, height=680, style={ + 'margin': '0px auto'}) + self.aidcam = OpencvVideoWidget(self, width=340, height=480) self.aidcam.style['margin'] = '10px' - - #HERE I setup a fixed identifier by which REMI will know where to redirect a post request + + # HERE I setup a fixed identifier by which REMI will know where to redirect a post request # The post request must specify in the header the widget identifier and the function name that will be used to pass the file data self.aidcam.set_identifier("myimage_receiver") main_container.append(self.aidcam) - self.lbl = gui.Label('This show FPS!', width=360, height=30, margin='10px') + self.lbl = gui.Label('This show FPS!', width=360, + height=30, margin='10px') main_container.append(self.lbl) return main_container - if __name__ == "__main__": - + initcv(main) startcv(MyApp) diff --git a/src/singlepose/webpose.py b/src/singlepose/webpose.py index 49d0294..a94b233 100644 --- a/src/singlepose/webpose.py +++ b/src/singlepose/webpose.py @@ -1,10 +1,10 @@ +import time +import numpy as np +import tensorflow as tf from cvs import * -print ('import tensorflow.....wait...') -import tensorflow as tf -import numpy as np -import time +print('import tensorflow.....wait...') imageSize = 257 width = imageSize @@ -21,92 +21,93 @@ def load_model(PATH_TO_CKPT): tf.import_graph_def(od_graph_def, name='') return detection_graph + def resizeimg(img, width, height): - img = cvs.resize(img, (width,height)) - + img = cvs.resize(img, (width, height)) + img = img.astype(float) img = img * (2.0 / 255.0) - 1.0 return img + def main(): - cam=cvs.VideoCapture(0) + cam = cvs.VideoCapture(0) detection_graph = load_model("frozen_model.pb") with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: image = detection_graph.get_tensor_by_name('image:0') - heatmaps=detection_graph.get_tensor_by_name('heatmap:0') - offsets=detection_graph.get_tensor_by_name('offset_2:0') - displacementFwd=detection_graph.get_tensor_by_name('displacement_fwd_2:0') - displacementBwd=detection_graph.get_tensor_by_name('displacement_bwd_2:0') - - - fcount=-1 + heatmaps = detection_graph.get_tensor_by_name('heatmap:0') + offsets = detection_graph.get_tensor_by_name('offset_2:0') + displacementFwd = detection_graph.get_tensor_by_name( + 'displacement_fwd_2:0') + displacementBwd = detection_graph.get_tensor_by_name( + 'displacement_bwd_2:0') + + fcount = -1 start = time.time() while True: sleep(30) img = cam.read() - if img is None : + if img is None: sleep(50) continue - fcount += 1 # global lbs - lbs = 'Average FPS: '+ str(fcount / (time.time() - start)) + lbs = 'Average FPS: ' + str(fcount / (time.time() - start)) cvs.setLbs(lbs) - - input_image = resizeimg(img,width,height) + input_image = resizeimg(img, width, height) tmpimg = img - tmpimg = cv2.resize(tmpimg, (width,height)) - - - input_image = np.array(input_image,dtype=np.float32) - input_image = input_image.reshape(1,width,height,3) - heatmaps_result,offsets_result,displacementFwd_result,displacementBwd_result = sess.run( - [heatmaps,offsets,displacementFwd,displacementBwd], feed_dict={ image: input_image } ) - colors = [[255, 0, 0], [255, 170, 0], [255, 170, 0],[255, 255, 0], [255, 255, 0], [170, 255, 0], [170, 255, 0], [0, 255, 0], - [0, 255, 0], [0, 255, 170], [0, 255, 170], [0, 170, 255], [0, 170, 255], [0, 0, 255], [0, 0, 255], - [255, 0, 255], [255, 0, 255]] - - - - pairs = [[5,6],[5,7],[6,8],[7,9],[8,10],[5,11],[6,12],[11,12],[11,13],[12,14],[13,15],[14,16]] + tmpimg = cv2.resize(tmpimg, (width, height)) + + input_image = np.array(input_image, dtype=np.float32) + input_image = input_image.reshape(1, width, height, 3) + heatmaps_result, offsets_result, displacementFwd_result, displacementBwd_result = sess.run( + [heatmaps, offsets, displacementFwd, displacementBwd], feed_dict={image: input_image}) + colors = [[255, 0, 0], [255, 170, 0], [255, 170, 0], [255, 255, 0], [255, 255, 0], [170, 255, 0], [170, 255, 0], [0, 255, 0], + [0, 255, 0], [0, 255, 170], [0, 255, 170], [ + 0, 170, 255], [0, 170, 255], [0, 0, 255], [0, 0, 255], + [255, 0, 255], [255, 0, 255]] + + pairs = [[5, 6], [5, 7], [6, 8], [7, 9], [8, 10], [5, 11], [ + 6, 12], [11, 12], [11, 13], [12, 14], [13, 15], [14, 16]] keypoint = [] ckeypoint = [] heatmaps_result = heatmaps_result[0] - aaaa= np.transpose(heatmaps_result,(2, 0, 1)) - offsets_result=offsets_result[0] - bbb= np.transpose(offsets_result,(2, 0, 1)) + aaaa = np.transpose(heatmaps_result, (2, 0, 1)) + offsets_result = offsets_result[0] + bbb = np.transpose(offsets_result, (2, 0, 1)) for k in range(17): - heatmaps_result=aaaa[k] - maxheat=np.max(heatmaps_result) - re=np.where(heatmaps_result==maxheat) - - ry=re[0][0] - rx=re[1][0] - offsets_resulty=bbb[0+k] - offsets_resultx=bbb[17+k] - ofx=int(offsets_resultx[ry][rx]+0.5) - ofy=int(offsets_resulty[ry][rx]+0.5) - - px=(rx)*16+ofx - py=(ry)*16+ofy - if maxheat>0.7 : - cvs.circle(tmpimg, (int(px), int(py)), 3, colors[k], -1) + heatmaps_result = aaaa[k] + maxheat = np.max(heatmaps_result) + re = np.where(heatmaps_result == maxheat) + + ry = re[0][0] + rx = re[1][0] + offsets_resulty = bbb[0+k] + offsets_resultx = bbb[17+k] + ofx = int(offsets_resultx[ry][rx]+0.5) + ofy = int(offsets_resulty[ry][rx]+0.5) + + px = (rx)*16+ofx + py = (ry)*16+ofy + if maxheat > 0.7: + cvs.circle(tmpimg, (int(px), int(py)), + 3, colors[k], -1) keypoint.append(int(px)) keypoint.append(int(py)) - else : - keypoint.append(-1); - keypoint.append(-1); + else: + keypoint.append(-1) + keypoint.append(-1) ckeypoint.append(keypoint[0]) ckeypoint.append(keypoint[1]) - for i in range(1,16,2): + for i in range(1, 16, 2): # if keypoint[2*i]>keypoint[2*(i+1)] and keypoint[2*(i+1)]>0 : # ckeypoint.append(keypoint[2*i+2]) # ckeypoint.append(keypoint[2*i+2+1]) @@ -119,39 +120,40 @@ def main(): ckeypoint.append(keypoint[2*i+2]) ckeypoint.append(keypoint[2*i+2+1]) - for pair in pairs: - if ckeypoint[2*pair[0]]>0 and ckeypoint[2*pair[1]]>0 : - cvs.line(tmpimg,(ckeypoint[2*pair[0]],ckeypoint[2*pair[0]+1]),(ckeypoint[2*pair[1]],ckeypoint[2*pair[1]+1]),(0,0,255),1) + for pair in pairs: + if ckeypoint[2*pair[0]] > 0 and ckeypoint[2*pair[1]] > 0: + cvs.line(tmpimg, (ckeypoint[2*pair[0]], ckeypoint[2*pair[0]+1]), + (ckeypoint[2*pair[1]], ckeypoint[2*pair[1]+1]), (0, 0, 255), 1) cvs.imshow(tmpimg) - print ('frames:',fcount) - + print('frames:', fcount) class MyApp(App): def __init__(self, *args): super(MyApp, self).__init__(*args) - + def idle(self): - #idle function called every update cycle - self.lbl.set_text(cvs.getLbs()) - + # idle function called every update cycle + self.lbl.set_text(cvs.getLbs()) + def main(self): - #creating a container VBox type, vertical (you can use also HBox or Widget) - main_container = gui.VBox(width=360, height=680, style={'margin':'0px auto'}) - + # creating a container VBox type, vertical (you can use also HBox or Widget) + main_container = gui.VBox(width=360, height=680, style={ + 'margin': '0px auto'}) + self.aidcam = OpencvVideoWidget(self, width=340, height=480) self.aidcam.style['margin'] = '10px' - + self.aidcam.set_identifier("myimage_receiver") main_container.append(self.aidcam) - - #label show fps - self.lbl = gui.Label('This show FPS!', width=360, height=30,left='100px', margin='10px') + + # label show fps + self.lbl = gui.Label('This show FPS!', width=360, + height=30, left='100px', margin='10px') main_container.append(self.lbl) return main_container - if __name__ == "__main__": From cc8413b1b936899570b8349d07983cfe89f1489d Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 9 Aug 2020 14:45:38 +0000 Subject: [PATCH 2/6] Restyled by black --- gui_cvs/table.py | 30 +++--- gui_wizard/helloworld.py | 94 +++++++++++++---- src/cvs/webcam.py | 20 ++-- src/facencnn/facepose.py | 32 +++--- src/handtf/utils/detector_utils.py | 117 +++++++++++++-------- src/handtf/webcam.py | 41 +++++--- src/mutilpose/posenet/converter/config.py | 2 +- src/mutilpose/posenet/decode_multi.py | 98 +++++++++++------- src/mutilpose/posenet/model.py | 30 +++--- src/mutilpose/posenet/utils.py | 93 ++++++++++++----- src/mutilpose/webcam_demo.py | 58 ++++++----- src/singlepose/webpose.py | 119 ++++++++++++++-------- 12 files changed, 474 insertions(+), 260 deletions(-) diff --git a/gui_cvs/table.py b/gui_cvs/table.py index 477ac7e..4ea58ae 100755 --- a/gui_cvs/table.py +++ b/gui_cvs/table.py @@ -6,39 +6,42 @@ def __init__(self, *args): super(MyApp, self).__init__(*args) def main(self): - wid = gui.VBox(width=500, height=500, style={ - 'margin': '5px auto', 'padding': '10px'}) + wid = gui.VBox( + width=500, height=500, style={"margin": "5px auto", "padding": "10px"} + ) - lbl_description = gui.Label("""Example about TableWidget usage. + lbl_description = gui.Label( + """Example about TableWidget usage. Change rows and columns count in order to see the behaviour. - After changing the size, 'Fill the table' content by means of the button.""") + After changing the size, 'Fill the table' content by means of the button.""" + ) wid.append(lbl_description) table = gui.TableWidget(10, 3, True, True, width=300, height=300) - table.style['font-size'] = '8px' + table.style["font-size"] = "8px" - container = gui.HBox(width='100%') - lbl_row_count = gui.Label('Rows:') + container = gui.HBox(width="100%") + lbl_row_count = gui.Label("Rows:") spin_row_count = gui.SpinBox(10, 0, 15) spin_row_count.onchange.do(self.on_row_count_change, table) container.append(lbl_row_count) container.append(spin_row_count) wid.append(container) - container = gui.HBox(width='100%') - lbl_column_count = gui.Label('Columns:') + container = gui.HBox(width="100%") + lbl_column_count = gui.Label("Columns:") spin_column_count = gui.SpinBox(3, 0, 4) spin_column_count.onchange.do(self.on_column_count_change, table) container.append(lbl_column_count) container.append(spin_column_count) wid.append(container) - bt_fill_table = gui.Button('Fill table', width=100) + bt_fill_table = gui.Button("Fill table", width=100) bt_fill_table.onclick.do(self.fill_table, table) wid.append(bt_fill_table) - chk_use_title = gui.CheckBoxLabel('Use title', True) + chk_use_title = gui.CheckBoxLabel("Use title", True) chk_use_title.onchange.do(self.on_use_title_change, table) wid.append(chk_use_title) @@ -59,11 +62,10 @@ def on_column_count_change(self, emitter, value, table): def fill_table(self, emitter, table): for ri in range(table.row_count()): for ci in range(table.column_count()): - table.item_at(ri, ci).set_text( - "row:%s,column:%s" % (str(ri), str(ci))) + table.item_at(ri, ci).set_text("row:%s,column:%s" % (str(ri), str(ci))) def on_use_title_change(self, emitter, value, table): - value = value == 'true' + value = value == "true" table.set_use_title(value) def on_table_item_changed(self, table, item, new_value, row, column): diff --git a/gui_wizard/helloworld.py b/gui_wizard/helloworld.py index 72f0ae7..8df59f8 100644 --- a/gui_wizard/helloworld.py +++ b/gui_wizard/helloworld.py @@ -1,4 +1,3 @@ - # -*- coding: utf-8 -*- @@ -11,9 +10,8 @@ class untitled(App): def __init__(self, *args, **kwargs): # DON'T MAKE CHANGES HERE, THIS METHOD GETS OVERWRITTEN WHEN SAVING IN THE EDITOR - if 'editing_mode' not in kwargs.keys(): - super(untitled, self).__init__( - *args, static_file_path={'my_res': './res/'}) + if "editing_mode" not in kwargs.keys(): + super(untitled, self).__init__(*args, static_file_path={"my_res": "./res/"}) def idle(self): # idle function called every update cycle @@ -26,33 +24,85 @@ def main(self): def construct_ui(self): # DON'T MAKE CHANGES HERE, THIS METHOD GETS OVERWRITTEN WHEN SAVING IN THE EDITOR mainContainer = Widget() - mainContainer.attributes.update({"editor_baseclass": "Widget", "editor_tag_type": "widget", "editor_newclass": "False", - "editor_constructor": "()", "class": "Widget", "editor_varname": "mainContainer"}) - mainContainer.style.update({"width": "266px", "position": "absolute", "top": "61px", - "left": "16px", "margin": "0px", "overflow": "auto", "height": "257px"}) - button = Button('Say Hello') - button.attributes.update({"editor_baseclass": "Button", "editor_tag_type": "widget", "editor_newclass": "False", - "editor_constructor": "('Say Hello')", "class": "Button", "editor_varname": "button"}) - button.style.update({"width": "149px", "position": "absolute", "top": "80px", - "left": "37px", "margin": "0px", "overflow": "auto", "height": "29px"}) - mainContainer.append(button, 'button') - label = Label('My label') - label.attributes.update({"editor_baseclass": "Label", "editor_tag_type": "widget", "editor_newclass": "False", - "editor_constructor": "('My label')", "class": "Label", "editor_varname": "label"}) - label.style.update({"width": "98px", "position": "absolute", "top": "20px", - "left": "20px", "margin": "0px", "overflow": "auto", "height": "25px"}) - mainContainer.append(label, 'label') - mainContainer.children['button'].onclick.do(self.onclick_button) + mainContainer.attributes.update( + { + "editor_baseclass": "Widget", + "editor_tag_type": "widget", + "editor_newclass": "False", + "editor_constructor": "()", + "class": "Widget", + "editor_varname": "mainContainer", + } + ) + mainContainer.style.update( + { + "width": "266px", + "position": "absolute", + "top": "61px", + "left": "16px", + "margin": "0px", + "overflow": "auto", + "height": "257px", + } + ) + button = Button("Say Hello") + button.attributes.update( + { + "editor_baseclass": "Button", + "editor_tag_type": "widget", + "editor_newclass": "False", + "editor_constructor": "('Say Hello')", + "class": "Button", + "editor_varname": "button", + } + ) + button.style.update( + { + "width": "149px", + "position": "absolute", + "top": "80px", + "left": "37px", + "margin": "0px", + "overflow": "auto", + "height": "29px", + } + ) + mainContainer.append(button, "button") + label = Label("My label") + label.attributes.update( + { + "editor_baseclass": "Label", + "editor_tag_type": "widget", + "editor_newclass": "False", + "editor_constructor": "('My label')", + "class": "Label", + "editor_varname": "label", + } + ) + label.style.update( + { + "width": "98px", + "position": "absolute", + "top": "20px", + "left": "20px", + "margin": "0px", + "overflow": "auto", + "height": "25px", + } + ) + mainContainer.append(label, "label") + mainContainer.children["button"].onclick.do(self.onclick_button) self.mainContainer = mainContainer return self.mainContainer def onclick_button(self, emitter): - self.mainContainer.children['label'].set_text('hello world') + self.mainContainer.children["label"].set_text("hello world") # processing_code + def process(): cap = cvs.VideoCapture(1) diff --git a/src/cvs/webcam.py b/src/cvs/webcam.py index c0c5960..4bed753 100644 --- a/src/cvs/webcam.py +++ b/src/cvs/webcam.py @@ -1,4 +1,3 @@ - # -*- coding: utf-8 -*- from cvs import * @@ -7,9 +6,8 @@ class untitled(App): def __init__(self, *args, **kwargs): # DON'T MAKE CHANGES HERE, THIS METHOD GETS OVERWRITTEN WHEN SAVING IN THE EDITOR - if 'editing_mode' not in kwargs.keys(): - super(untitled, self).__init__( - *args, static_file_path={'my_res': './res/'}) + if "editing_mode" not in kwargs.keys(): + super(untitled, self).__init__(*args, static_file_path={"my_res": "./res/"}) def idle(self): # idle function called every update cycle @@ -24,8 +22,17 @@ def construct_ui(self): aidcam = OpencvVideoWidget(self) # aidcam.attributes.update({"src":"/528175722896/get_image_data","editor_newclass":"False","editor_baseclass":"OpencvVideoWidget","editor_constructor":"()","class":"OpencvVideoWidget","editor_tag_type":"widget","editor_varname":"aidcam"}) aidcam.set_identifier("myimage_receiver") - aidcam.style.update({"width": "190px", "position": "absolute", "top": "20px", - "left": "20px", "margin": "0px", "overflow": "auto", "height": "177px"}) + aidcam.style.update( + { + "width": "190px", + "position": "absolute", + "top": "20px", + "left": "20px", + "margin": "0px", + "overflow": "auto", + "height": "177px", + } + ) self.aidcam = aidcam return self.aidcam @@ -33,6 +40,7 @@ def construct_ui(self): # processing_code + def process(): cap = cvs.VideoCapture(1) diff --git a/src/facencnn/facepose.py b/src/facencnn/facepose.py index 67ce9b7..308871e 100644 --- a/src/facencnn/facepose.py +++ b/src/facencnn/facepose.py @@ -20,7 +20,7 @@ def main(): fcount += 1 # global lbs - lbs = 'Average FPS: ' + str(fcount / (time.time() - start)) + lbs = "Average FPS: " + str(fcount / (time.time() - start)) cvs.setLbs(lbs) if camid == 1: @@ -33,26 +33,26 @@ def main(): # print 'rets:',rets for ret in rets: # for ret in each: - print('draw bounding box for the face') - #cvs.infoshow('draw bounding box for the face') - rect = ret['rect'] + print("draw bounding box for the face") + # cvs.infoshow('draw bounding box for the face') + rect = ret["rect"] # print rect - mtcnn = ret['mtcnn'] + mtcnn = ret["mtcnn"] # print mtcnn for i in range(5): - cvs.circle(img, (mtcnn[i], mtcnn[5+i]), 2, (0, 0, 255), 2) - keypoint = ret['keypoints'] + cvs.circle(img, (mtcnn[i], mtcnn[5 + i]), 2, (0, 0, 255), 2) + keypoint = ret["keypoints"] # print keypoint p1 = (int(rect[0]), int(rect[1])) - p2 = (int(rect[0]+rect[2]), int(rect[1]+rect[3])) + p2 = (int(rect[0] + rect[2]), int(rect[1] + rect[3])) - #draw_name(img, rect, ret['name']) + # draw_name(img, rect, ret['name']) cvs.rectangle(img, p1, p2, (0, 255, 0), 3, 1) for p in range(106): # print p*2,' = ',keypoint[p*2] # print p*2+1,' = ',keypoint[p*2+1] - k1 = int(rect[0]+keypoint[p*2]) - k2 = int(rect[1]+keypoint[p*2+1]) + k1 = int(rect[0] + keypoint[p * 2]) + k2 = int(rect[1] + keypoint[p * 2 + 1]) cv2.circle(img, (k1, k2), 2, (253, 0, 0), 2) cvs.imshow(img) @@ -68,22 +68,20 @@ def idle(self): def main(self): # creating a container VBox type, vertical (you can use also HBox or Widget) - main_container = gui.VBox(width=360, height=680, style={ - 'margin': '0px auto'}) + main_container = gui.VBox(width=360, height=680, style={"margin": "0px auto"}) self.aidcam = OpencvVideoWidget(self, width=340, height=480) - self.aidcam.style['margin'] = '10px' + self.aidcam.style["margin"] = "10px" self.aidcam.set_identifier("myimage_receiver") main_container.append(self.aidcam) - self.lbl = gui.Label('This show fps!', width=360, - height=30, margin='50px',) + self.lbl = gui.Label("This show fps!", width=360, height=30, margin="50px",) main_container.append(self.lbl) return main_container -if __name__ == '__main__': +if __name__ == "__main__": initcv(main) startcv(MyApp) diff --git a/src/handtf/utils/detector_utils.py b/src/handtf/utils/detector_utils.py index 7f176ab..24cc502 100644 --- a/src/handtf/utils/detector_utils.py +++ b/src/handtf/utils/detector_utils.py @@ -12,17 +12,18 @@ detection_graph = tf.Graph() -TRAINED_MODEL_DIR = 'frozen_graphs' +TRAINED_MODEL_DIR = "frozen_graphs" # Path to frozen detection graph. This is the actual model that is used for the object detection. -PATH_TO_CKPT = TRAINED_MODEL_DIR + '/ssd5_optimized_inference_graph.pb' +PATH_TO_CKPT = TRAINED_MODEL_DIR + "/ssd5_optimized_inference_graph.pb" # List of the strings that is used to add correct label for each box. -PATH_TO_LABELS = TRAINED_MODEL_DIR + '/hand_label_map.pbtxt' +PATH_TO_LABELS = TRAINED_MODEL_DIR + "/hand_label_map.pbtxt" NUM_CLASSES = 2 # load label map using utils provided by tensorflow object detection api label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( - label_map, max_num_classes=NUM_CLASSES, use_display_name=True) + label_map, max_num_classes=NUM_CLASSES, use_display_name=True +) category_index = label_map_util.create_category_index(categories) @@ -34,15 +35,17 @@ def load_inference_graph(): detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() - with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: + with tf.gfile.GFile(PATH_TO_CKPT, "rb") as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) - tf.import_graph_def(od_graph_def, name='') + tf.import_graph_def(od_graph_def, name="") - config = tf.ConfigProto(device_count={"CPU": 4}, - inter_op_parallelism_threads=1, - intra_op_parallelism_threads=4, - log_device_placement=True) + config = tf.ConfigProto( + device_count={"CPU": 4}, + inter_op_parallelism_threads=1, + intra_op_parallelism_threads=4, + log_device_placement=True, + ) sess = tf.Session(config=config, graph=detection_graph) print("> ====== Inference graph loaded.") @@ -50,7 +53,16 @@ def load_inference_graph(): # Drawing bounding boxes and distances onto image -def draw_box_on_image(num_hands_detect, score_thresh, scores, boxes, classes, im_width, im_height, image_np): +def draw_box_on_image( + num_hands_detect, + score_thresh, + scores, + boxes, + classes, + im_width, + im_height, + image_np, +): # Determined using a piece of paper of known length, code can be found in distance to camera focalLength = 875 # The average width of a human hand (inches) http://www.theaveragebody.com/average_hand_size.php @@ -61,67 +73,90 @@ def draw_box_on_image(num_hands_detect, score_thresh, scores, boxes, classes, im color0 = (255, 0, 0) color1 = (0, 50, 255) for i in range(num_hands_detect): - if (scores[i] > score_thresh): + if scores[i] > score_thresh: if classes[i] == 1: - id = 'open' + id = "open" if classes[i] == 2: - id = 'closed' + id = "closed" avg_width = 3.0 # To compensate bbox size change color = color0 if i == 0 else color1 - (left, right, top, bottom) = (boxes[i][1] * im_width, boxes[i][3] * im_width, - boxes[i][0] * im_height, boxes[i][2] * im_height) + (left, right, top, bottom) = ( + boxes[i][1] * im_width, + boxes[i][3] * im_width, + boxes[i][0] * im_height, + boxes[i][2] * im_height, + ) p1 = (int(left), int(top)) p2 = (int(right), int(bottom)) - dist = distance_to_camera(avg_width, focalLength, int(right-left)) + dist = distance_to_camera(avg_width, focalLength, int(right - left)) cv2.rectangle(image_np, p1, p2, color, 3, 1) - cv2.putText(image_np, 'hand '+str(i)+': '+id, (int(left), int(top)-5), - cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) - - cv2.putText(image_np, 'confidence: '+str("{0:.2f}".format(scores[i])), - (int(left), int(top)-20), - cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) - - cv2.putText(image_np, 'distance: '+str("{0:.2f}".format(dist)+' inches'), - (int(im_width*0.7), int(im_height*0.9+30*i)), - cv2.FONT_HERSHEY_SIMPLEX, 0.75, color, 2) + cv2.putText( + image_np, + "hand " + str(i) + ": " + id, + (int(left), int(top) - 5), + cv2.FONT_HERSHEY_SIMPLEX, + 0.5, + color, + 2, + ) + + cv2.putText( + image_np, + "confidence: " + str("{0:.2f}".format(scores[i])), + (int(left), int(top) - 20), + cv2.FONT_HERSHEY_SIMPLEX, + 0.5, + (0, 255, 0), + 2, + ) + + cv2.putText( + image_np, + "distance: " + str("{0:.2f}".format(dist) + " inches"), + (int(im_width * 0.7), int(im_height * 0.9 + 30 * i)), + cv2.FONT_HERSHEY_SIMPLEX, + 0.75, + color, + 2, + ) # Show fps value on image. def draw_text_on_image(fps, image_np): - cv2.putText(image_np, fps, (20, 50), - cv2.FONT_HERSHEY_SIMPLEX, 0.75, (77, 255, 9), 2) + cv2.putText( + image_np, fps, (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (77, 255, 9), 2 + ) + + # compute and return the distance from the hand to the camera using triangle similarity def distance_to_camera(knownWidth, focalLength, pixelWidth): return (knownWidth * focalLength) / pixelWidth + # Actual detection .. generate scores and bounding boxes given an image def detect_objects(image_np, detection_graph, sess): # Definite input and output Tensors for detection_graph - image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') + image_tensor = detection_graph.get_tensor_by_name("image_tensor:0") # Each box represents a part of the image where a particular object was detected. - detection_boxes = detection_graph.get_tensor_by_name( - 'detection_boxes:0') + detection_boxes = detection_graph.get_tensor_by_name("detection_boxes:0") # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. - detection_scores = detection_graph.get_tensor_by_name( - 'detection_scores:0') - detection_classes = detection_graph.get_tensor_by_name( - 'detection_classes:0') - num_detections = detection_graph.get_tensor_by_name( - 'num_detections:0') + detection_scores = detection_graph.get_tensor_by_name("detection_scores:0") + detection_classes = detection_graph.get_tensor_by_name("detection_classes:0") + num_detections = detection_graph.get_tensor_by_name("num_detections:0") image_np_expanded = np.expand_dims(image_np, axis=0) (boxes, scores, classes, num) = sess.run( - [detection_boxes, detection_scores, - detection_classes, num_detections], - feed_dict={image_tensor: image_np_expanded}) + [detection_boxes, detection_scores, detection_classes, num_detections], + feed_dict={image_tensor: image_np_expanded}, + ) return np.squeeze(boxes), np.squeeze(scores), np.squeeze(classes) diff --git a/src/handtf/webcam.py b/src/handtf/webcam.py index ab5ee6e..9db7e94 100644 --- a/src/handtf/webcam.py +++ b/src/handtf/webcam.py @@ -6,8 +6,14 @@ from cvs import * ap = argparse.ArgumentParser() -ap.add_argument('-d', '--display', dest='display', type=int, - default=1, help='Display the detected images using OpenCV. This reduces FPS') +ap.add_argument( + "-d", + "--display", + dest="display", + type=int, + default=1, + help="Display the detected images using OpenCV. This reduces FPS", +) args = vars(ap.parse_args()) @@ -49,26 +55,35 @@ def main(): # Run image through tensorflow graph boxes, scores, classes = detector_utils.detect_objects( - frame, detection_graph, sess) + frame, detection_graph, sess + ) # Draw bounding boxeses and text detector_utils.draw_box_on_image( - num_hands_detect, score_thresh, scores, boxes, classes, im_width, im_height, frame) + num_hands_detect, + score_thresh, + scores, + boxes, + classes, + im_width, + im_height, + frame, + ) # Calculate Frames per second (FPS) num_frames += 1 - elapsed_time = (datetime.datetime.now() - - start_time).total_seconds() + elapsed_time = (datetime.datetime.now() - start_time).total_seconds() fps = num_frames / elapsed_time # Display FPS on frame lbs = "FPS : " + str("{0:.2f}".format(fps)) cvs.setLbs(lbs) - if args['display']: + if args["display"]: detector_utils.draw_text_on_image( - "FPS : " + str("{0:.2f}".format(fps)), frame) + "FPS : " + str("{0:.2f}".format(fps)), frame + ) cvs.imshow(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)) @@ -89,23 +104,21 @@ def idle(self): def main(self): # initcv(process) # creating a container VBox type, vertical (you can use also HBox or Widget) - main_container = gui.VBox(width=360, height=680, style={ - 'margin': '0px auto'}) + main_container = gui.VBox(width=360, height=680, style={"margin": "0px auto"}) self.aidcam = OpencvVideoWidget(self, width=340, height=480) - self.aidcam.style['margin'] = '10px' + self.aidcam.style["margin"] = "10px" self.aidcam.set_identifier("myimage_receiver") main_container.append(self.aidcam) # Display FPS on frame - self.lbl = gui.Label('This is a LABEL!', width=360, - height=30, margin='10px') + self.lbl = gui.Label("This is a LABEL!", width=360, height=30, margin="10px") main_container.append(self.lbl) return main_container -if __name__ == '__main__': +if __name__ == "__main__": initcv(main) startcv(MyApp) diff --git a/src/mutilpose/posenet/converter/config.py b/src/mutilpose/posenet/converter/config.py index 8a61c1b..cbf2b6e 100644 --- a/src/mutilpose/posenet/converter/config.py +++ b/src/mutilpose/posenet/converter/config.py @@ -4,6 +4,6 @@ BASE_DIR = os.path.dirname(__file__) -def load_config(config_name='config.yaml'): +def load_config(config_name="config.yaml"): cfg_f = open(os.path.join(BASE_DIR, config_name), "r+") return yaml.load(cfg_f) diff --git a/src/mutilpose/posenet/decode_multi.py b/src/mutilpose/posenet/decode_multi.py index b6d7fbb..856521b 100644 --- a/src/mutilpose/posenet/decode_multi.py +++ b/src/mutilpose/posenet/decode_multi.py @@ -18,32 +18,38 @@ def within_nms_radius_fast(pose_coords, squared_nms_radius, point): def get_instance_score( - existing_poses, squared_nms_radius, - keypoint_scores, keypoint_coords): - not_overlapped_scores = 0. + existing_poses, squared_nms_radius, keypoint_scores, keypoint_coords +): + not_overlapped_scores = 0.0 for keypoint_id in range(len(keypoint_scores)): if not within_nms_radius( - existing_poses, squared_nms_radius, - keypoint_coords[keypoint_id], keypoint_id): + existing_poses, + squared_nms_radius, + keypoint_coords[keypoint_id], + keypoint_id, + ): not_overlapped_scores += keypoint_scores[keypoint_id] return not_overlapped_scores / len(keypoint_scores) def get_instance_score_fast( - exist_pose_coords, - squared_nms_radius, - keypoint_scores, keypoint_coords): + exist_pose_coords, squared_nms_radius, keypoint_scores, keypoint_coords +): if exist_pose_coords.shape[0]: - s = np.sum((exist_pose_coords - keypoint_coords) - ** 2, axis=2) > squared_nms_radius + s = ( + np.sum((exist_pose_coords - keypoint_coords) ** 2, axis=2) + > squared_nms_radius + ) not_overlapped_scores = np.sum(keypoint_scores[np.all(s, axis=0)]) else: not_overlapped_scores = np.sum(keypoint_scores) return not_overlapped_scores / len(keypoint_scores) -def score_is_max_in_local_window(keypoint_id, score, hmy, hmx, local_max_radius, scores): +def score_is_max_in_local_window( + keypoint_id, score, hmy, hmx, local_max_radius, scores +): height = scores.shape[0] width = scores.shape[1] @@ -71,11 +77,10 @@ def build_part_with_score(score_threshold, local_max_radius, scores): score = scores[hmy, hmx, keypoint_id] if score < score_threshold: continue - if score_is_max_in_local_window(keypoint_id, score, hmy, hmx, - local_max_radius, scores): - parts.append(( - score, keypoint_id, np.array((hmy, hmx)) - )) + if score_is_max_in_local_window( + keypoint_id, score, hmy, hmx, local_max_radius, scores + ): + parts.append((score, keypoint_id, np.array((hmy, hmx)))) return parts @@ -88,23 +93,27 @@ def build_part_with_score_fast(score_threshold, local_max_radius, scores): # on each subarray vs doing the op on the full score array with size=(lmd, lmd, 1) for keypoint_id in range(num_keypoints): kp_scores = scores[:, :, keypoint_id].copy() - kp_scores[kp_scores < score_threshold] = 0. - max_vals = ndi.maximum_filter(kp_scores, size=lmd, mode='constant') + kp_scores[kp_scores < score_threshold] = 0.0 + max_vals = ndi.maximum_filter(kp_scores, size=lmd, mode="constant") max_loc = np.logical_and(kp_scores == max_vals, kp_scores > 0) max_loc_idx = max_loc.nonzero() for y, x in zip(*max_loc_idx): - parts.append(( - scores[y, x, keypoint_id], - keypoint_id, - np.array((y, x)) - )) + parts.append((scores[y, x, keypoint_id], keypoint_id, np.array((y, x)))) return parts def decode_multiple_poses( - scores, offsets, displacements_fwd, displacements_bwd, output_stride, - max_pose_detections=10, score_threshold=0.5, nms_radius=20, min_pose_score=0.5): + scores, + offsets, + displacements_fwd, + displacements_bwd, + output_stride, + max_pose_detections=10, + score_threshold=0.5, + nms_radius=20, + min_pose_score=0.5, +): pose_count = 0 pose_scores = np.zeros(max_pose_detections) @@ -114,39 +123,52 @@ def decode_multiple_poses( squared_nms_radius = nms_radius ** 2 scored_parts = build_part_with_score_fast( - score_threshold, LOCAL_MAXIMUM_RADIUS, scores) + score_threshold, LOCAL_MAXIMUM_RADIUS, scores + ) scored_parts = sorted(scored_parts, key=lambda x: x[0], reverse=True) # change dimensions from (h, w, x) to (h, w, x//2, 2) to allow return of complete coord array height = scores.shape[0] width = scores.shape[1] offsets = offsets.reshape(height, width, 2, -1).swapaxes(2, 3) - displacements_fwd = displacements_fwd.reshape( - height, width, 2, -1).swapaxes(2, 3) - displacements_bwd = displacements_bwd.reshape( - height, width, 2, -1).swapaxes(2, 3) + displacements_fwd = displacements_fwd.reshape(height, width, 2, -1).swapaxes(2, 3) + displacements_bwd = displacements_bwd.reshape(height, width, 2, -1).swapaxes(2, 3) for root_score, root_id, root_coord in scored_parts: - root_image_coords = root_coord * output_stride + offsets[ - root_coord[0], root_coord[1], root_id] + root_image_coords = ( + root_coord * output_stride + offsets[root_coord[0], root_coord[1], root_id] + ) if within_nms_radius_fast( - pose_keypoint_coords[:pose_count, root_id, :], squared_nms_radius, root_image_coords): + pose_keypoint_coords[:pose_count, root_id, :], + squared_nms_radius, + root_image_coords, + ): continue keypoint_scores, keypoint_coords = decode_pose( - root_score, root_id, root_image_coords, - scores, offsets, output_stride, - displacements_fwd, displacements_bwd) + root_score, + root_id, + root_image_coords, + scores, + offsets, + output_stride, + displacements_fwd, + displacements_bwd, + ) pose_score = get_instance_score_fast( - pose_keypoint_coords[:pose_count, :, :], squared_nms_radius, keypoint_scores, keypoint_coords) + pose_keypoint_coords[:pose_count, :, :], + squared_nms_radius, + keypoint_scores, + keypoint_coords, + ) # NOTE this isn't in the original implementation, but it appears that by initially ordering by # part scores, and having a max # of detections, we can end up populating the returned poses with # lower scored poses than if we discard 'bad' ones and continue (higher pose scores can still come later). # Set min_pose_score to 0. to revert to original behaviour - if min_pose_score == 0. or pose_score >= min_pose_score: + if min_pose_score == 0.0 or pose_score >= min_pose_score: pose_scores[pose_count] = pose_score pose_keypoint_scores[pose_count, :] = keypoint_scores pose_keypoint_coords[pose_count, :, :] = keypoint_coords diff --git a/src/mutilpose/posenet/model.py b/src/mutilpose/posenet/model.py index d650ce4..a473680 100644 --- a/src/mutilpose/posenet/model.py +++ b/src/mutilpose/posenet/model.py @@ -2,7 +2,7 @@ import os import posenet.converter.config -MODEL_DIR = './_models' +MODEL_DIR = "./_models" DEBUG_OUTPUT = False @@ -21,43 +21,43 @@ def model_id_to_ord(model_id): def load_config(model_ord): converter_cfg = posenet.converter.config.load_config() - checkpoints = converter_cfg['checkpoints'] - output_stride = converter_cfg['outputStride'] + checkpoints = converter_cfg["checkpoints"] + output_stride = converter_cfg["outputStride"] checkpoint_name = checkpoints[model_ord] return { - 'output_stride': output_stride, - 'checkpoint_name': checkpoint_name, + "output_stride": output_stride, + "checkpoint_name": checkpoint_name, } def load_model(model_id, sess, model_dir=MODEL_DIR): model_ord = model_id_to_ord(model_id) model_cfg = load_config(model_ord) - model_path = os.path.join(model_dir, 'model-%s.pb' % - model_cfg['checkpoint_name']) + model_path = os.path.join(model_dir, "model-%s.pb" % model_cfg["checkpoint_name"]) if not os.path.exists(model_path): - print('Cannot find model file %s, converting from tfjs...' % model_path) + print("Cannot find model file %s, converting from tfjs..." % model_path) from posenet.converter.tfjs2python import convert + convert(model_ord, model_dir, check=False) assert os.path.exists(model_path) - with tf.gfile.GFile(model_path, 'rb') as f: + with tf.gfile.GFile(model_path, "rb") as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) sess.graph.as_default() - tf.import_graph_def(graph_def, name='') + tf.import_graph_def(graph_def, name="") if DEBUG_OUTPUT: graph_nodes = [n for n in graph_def.node] names = [] for t in graph_nodes: names.append(t.name) - print('Loaded graph node:', t.name) + print("Loaded graph node:", t.name) - offsets = sess.graph.get_tensor_by_name('offset_2:0') - displacement_fwd = sess.graph.get_tensor_by_name('displacement_fwd_2:0') - displacement_bwd = sess.graph.get_tensor_by_name('displacement_bwd_2:0') - heatmaps = sess.graph.get_tensor_by_name('heatmap:0') + offsets = sess.graph.get_tensor_by_name("offset_2:0") + displacement_fwd = sess.graph.get_tensor_by_name("displacement_fwd_2:0") + displacement_bwd = sess.graph.get_tensor_by_name("displacement_bwd_2:0") + heatmaps = sess.graph.get_tensor_by_name("heatmap:0") return model_cfg, [heatmaps, offsets, displacement_fwd, displacement_bwd] diff --git a/src/mutilpose/posenet/utils.py b/src/mutilpose/posenet/utils.py index 8052d5a..cc41065 100644 --- a/src/mutilpose/posenet/utils.py +++ b/src/mutilpose/posenet/utils.py @@ -3,6 +3,7 @@ import posenet.constants import sys + scale_factor = 1.0 if sys.version_info < (3, 5): scale_factor = 0.7125 @@ -16,12 +17,17 @@ def valid_resolution(width, height, output_stride=16): def _process_input(source_img, scale_factor=1.0, output_stride=16): target_width, target_height = valid_resolution( - source_img.shape[1] * scale_factor, source_img.shape[0] * scale_factor, output_stride=output_stride) - scale = np.array([source_img.shape[0] / target_height, - source_img.shape[1] / target_width]) + source_img.shape[1] * scale_factor, + source_img.shape[0] * scale_factor, + output_stride=output_stride, + ) + scale = np.array( + [source_img.shape[0] / target_height, source_img.shape[1] / target_width] + ) input_img = cv2.resize( - source_img, (target_width, target_height), interpolation=cv2.INTER_LINEAR) + source_img, (target_width, target_height), interpolation=cv2.INTER_LINEAR + ) input_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB).astype(np.float32) input_img = input_img * (2.0 / 255.0) - 1.0 input_img = input_img.reshape(1, target_height, target_width, 3) @@ -29,7 +35,7 @@ def _process_input(source_img, scale_factor=1.0, output_stride=16): def read_cap(img, scale_factor=1.0, output_stride=16): - #res, img = cap.read() + # res, img = cap.read() # if not res: # raise IOError("webcam failure") return _process_input(img, scale_factor, output_stride) @@ -41,8 +47,13 @@ def read_imgfile(path, scale_factor=1.0, output_stride=16): def draw_keypoints( - img, instance_scores, keypoint_scores, keypoint_coords, - min_pose_confidence=0.5, min_part_confidence=0.5): + img, + instance_scores, + keypoint_scores, + keypoint_coords, + min_pose_confidence=0.5, + min_part_confidence=0.5, +): cv_keypoints = [] for ii, score in enumerate(instance_scores): if score < min_pose_confidence: @@ -50,41 +61,60 @@ def draw_keypoints( for ks, kc in zip(keypoint_scores[ii, :], keypoint_coords[ii, :, :]): if ks < min_part_confidence: continue - cv_keypoints.append(cv2.KeyPoint(kc[1], kc[0], 10. * ks)) + cv_keypoints.append(cv2.KeyPoint(kc[1], kc[0], 10.0 * ks)) return cv2.drawKeypoints(img, cv_keypoints, outImage=np.array([])) def get_adjacent_keypoints(keypoint_scores, keypoint_coords, min_confidence=0.1): results = [] for left, right in posenet.CONNECTED_PART_INDICES: - if keypoint_scores[left] < min_confidence or keypoint_scores[right] < min_confidence: + if ( + keypoint_scores[left] < min_confidence + or keypoint_scores[right] < min_confidence + ): continue results.append( - np.array([keypoint_coords[left][::-1]/scale_factor, - keypoint_coords[right][::-1]/scale_factor]).astype(np.int32), + np.array( + [ + keypoint_coords[left][::-1] / scale_factor, + keypoint_coords[right][::-1] / scale_factor, + ] + ).astype(np.int32), ) return results def draw_skeleton( - img, instance_scores, keypoint_scores, keypoint_coords, - min_pose_confidence=0.5, min_part_confidence=0.5): + img, + instance_scores, + keypoint_scores, + keypoint_coords, + min_pose_confidence=0.5, + min_part_confidence=0.5, +): out_img = img adjacent_keypoints = [] for ii, score in enumerate(instance_scores): if score < min_pose_confidence: continue new_keypoints = get_adjacent_keypoints( - keypoint_scores[ii, :], keypoint_coords[ii, :, :], min_part_confidence) + keypoint_scores[ii, :], keypoint_coords[ii, :, :], min_part_confidence + ) adjacent_keypoints.extend(new_keypoints) - out_img = cv2.polylines(out_img, adjacent_keypoints, - isClosed=False, color=(255, 255, 0)) + out_img = cv2.polylines( + out_img, adjacent_keypoints, isClosed=False, color=(255, 255, 0) + ) return out_img def draw_skel_and_kp( - img, instance_scores, keypoint_scores, keypoint_coords, - min_pose_score=0.3, min_part_score=0.3): + img, + instance_scores, + keypoint_scores, + keypoint_coords, + min_pose_score=0.3, + min_part_score=0.3, +): out_img = img adjacent_keypoints = [] cv_keypoints = [] @@ -93,26 +123,37 @@ def draw_skel_and_kp( continue new_keypoints = get_adjacent_keypoints( - keypoint_scores[ii, :], keypoint_coords[ii, :, :], min_part_score) + keypoint_scores[ii, :], keypoint_coords[ii, :, :], min_part_score + ) adjacent_keypoints.extend(new_keypoints) for ks, kc in zip(keypoint_scores[ii, :], keypoint_coords[ii, :, :]): if ks < min_part_score: continue - cv_keypoints.append(cv2.KeyPoint( - kc[1]/scale_factor, kc[0]/scale_factor, 10. * ks)) + cv_keypoints.append( + cv2.KeyPoint(kc[1] / scale_factor, kc[0] / scale_factor, 10.0 * ks) + ) # out_img = cv2.drawKeypoints( # out_img, cv_keypoints, np.array([]), (0, 0, 255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # return out_img - out_img2 = cv2.polylines( - out_img, adjacent_keypoints, False, (255, 255, 0), 2) + out_img2 = cv2.polylines(out_img, adjacent_keypoints, False, (255, 255, 0), 2) # out_img1 = cv2.drawKeypoints( - # out_img, cv_keypoints, np.array([]), (0, 0, 255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) + # out_img, cv_keypoints, np.array([]), (0, 0, 255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) if out_img2 is None: out_img1 = cv2.drawKeypoints( - out_img, cv_keypoints, np.array([]), (0, 0, 255), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) + out_img, + cv_keypoints, + np.array([]), + (0, 0, 255), + flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS, + ) return out_img1 out_img1 = cv2.drawKeypoints( - out_img2, cv_keypoints, np.array([]), (0, 0, 255), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) + out_img2, + cv_keypoints, + np.array([]), + (0, 0, 255), + flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS, + ) return out_img1 diff --git a/src/mutilpose/webcam_demo.py b/src/mutilpose/webcam_demo.py index 9e10754..31d0b2e 100644 --- a/src/mutilpose/webcam_demo.py +++ b/src/mutilpose/webcam_demo.py @@ -1,19 +1,18 @@ - import posenet import argparse import time import tensorflow as tf from cvs import * -print('import tensorflow...wait...') +print("import tensorflow...wait...") parser = argparse.ArgumentParser() -parser.add_argument('--model', type=int, default=50) -parser.add_argument('--cam', type=int, default=0) -parser.add_argument('--cam_width', type=int, default=640) -parser.add_argument('--cam_height', type=int, default=480) -parser.add_argument('--scale_factor', type=float, default=0.7125) # 0.7125 +parser.add_argument("--model", type=int, default=50) +parser.add_argument("--cam", type=int, default=0) +parser.add_argument("--cam_width", type=int, default=640) +parser.add_argument("--cam_height", type=int, default=480) +parser.add_argument("--scale_factor", type=float, default=0.7125) # 0.7125 args = parser.parse_args() cam_id = args.cam @@ -23,9 +22,9 @@ def main(): cap = cvs.VideoCapture(0) with tf.Session() as sess: - print('load models...') + print("load models...") model_cfg, model_outputs = posenet.load_model(args.model, sess) - output_stride = model_cfg['output_stride'] + output_stride = model_cfg["output_stride"] start = time.time() frame_count = 0 @@ -40,34 +39,47 @@ def main(): img = cvs.flip(img, 0) input_image, display_image, output_scale = posenet.read_cap( - img, scale_factor=args.scale_factor, output_stride=output_stride) - - heatmaps_result, offsets_result, displacement_fwd_result, displacement_bwd_result = sess.run( - model_outputs, - feed_dict={'image:0': input_image} + img, scale_factor=args.scale_factor, output_stride=output_stride ) - pose_scores, keypoint_scores, keypoint_coords = posenet.decode_multi.decode_multiple_poses( + ( + heatmaps_result, + offsets_result, + displacement_fwd_result, + displacement_bwd_result, + ) = sess.run(model_outputs, feed_dict={"image:0": input_image}) + + ( + pose_scores, + keypoint_scores, + keypoint_coords, + ) = posenet.decode_multi.decode_multiple_poses( heatmaps_result.squeeze(axis=0), offsets_result.squeeze(axis=0), displacement_fwd_result.squeeze(axis=0), displacement_bwd_result.squeeze(axis=0), output_stride=output_stride, max_pose_detections=10, - min_pose_score=0.15) + min_pose_score=0.15, + ) keypoint_coords *= output_scale # print keypoint_coords # TODO this isn't particularly fast, use GL for drawing and display someday... overlay_image = posenet.draw_skel_and_kp( - display_image, pose_scores, keypoint_scores, keypoint_coords, - min_pose_score=0.15, min_part_score=0.1) + display_image, + pose_scores, + keypoint_scores, + keypoint_coords, + min_pose_score=0.15, + min_part_score=0.1, + ) cvs.imshow(overlay_image) frame_count += 1 # global lbs - lbs = 'Average FPS: ' + str(frame_count / (time.time() - start)) + lbs = "Average FPS: " + str(frame_count / (time.time() - start)) cvs.setLbs(lbs) @@ -81,18 +93,16 @@ def idle(self): def main(self): # creating a container VBox type, vertical (you can use also HBox or Widget) - main_container = gui.VBox(width=360, height=680, style={ - 'margin': '0px auto'}) + main_container = gui.VBox(width=360, height=680, style={"margin": "0px auto"}) self.aidcam = OpencvVideoWidget(self, width=340, height=480) - self.aidcam.style['margin'] = '10px' + self.aidcam.style["margin"] = "10px" # HERE I setup a fixed identifier by which REMI will know where to redirect a post request # The post request must specify in the header the widget identifier and the function name that will be used to pass the file data self.aidcam.set_identifier("myimage_receiver") main_container.append(self.aidcam) - self.lbl = gui.Label('This show FPS!', width=360, - height=30, margin='10px') + self.lbl = gui.Label("This show FPS!", width=360, height=30, margin="10px") main_container.append(self.lbl) return main_container diff --git a/src/singlepose/webpose.py b/src/singlepose/webpose.py index a94b233..a20220e 100644 --- a/src/singlepose/webpose.py +++ b/src/singlepose/webpose.py @@ -1,10 +1,9 @@ - import time import numpy as np import tensorflow as tf from cvs import * -print('import tensorflow.....wait...') +print("import tensorflow.....wait...") imageSize = 257 width = imageSize @@ -15,10 +14,10 @@ def load_model(PATH_TO_CKPT): detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() - with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: + with tf.gfile.GFile(PATH_TO_CKPT, "rb") as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) - tf.import_graph_def(od_graph_def, name='') + tf.import_graph_def(od_graph_def, name="") return detection_graph @@ -36,13 +35,11 @@ def main(): detection_graph = load_model("frozen_model.pb") with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: - image = detection_graph.get_tensor_by_name('image:0') - heatmaps = detection_graph.get_tensor_by_name('heatmap:0') - offsets = detection_graph.get_tensor_by_name('offset_2:0') - displacementFwd = detection_graph.get_tensor_by_name( - 'displacement_fwd_2:0') - displacementBwd = detection_graph.get_tensor_by_name( - 'displacement_bwd_2:0') + image = detection_graph.get_tensor_by_name("image:0") + heatmaps = detection_graph.get_tensor_by_name("heatmap:0") + offsets = detection_graph.get_tensor_by_name("offset_2:0") + displacementFwd = detection_graph.get_tensor_by_name("displacement_fwd_2:0") + displacementBwd = detection_graph.get_tensor_by_name("displacement_bwd_2:0") fcount = -1 start = time.time() @@ -55,7 +52,7 @@ def main(): fcount += 1 # global lbs - lbs = 'Average FPS: ' + str(fcount / (time.time() - start)) + lbs = "Average FPS: " + str(fcount / (time.time() - start)) cvs.setLbs(lbs) input_image = resizeimg(img, width, height) @@ -64,15 +61,49 @@ def main(): input_image = np.array(input_image, dtype=np.float32) input_image = input_image.reshape(1, width, height, 3) - heatmaps_result, offsets_result, displacementFwd_result, displacementBwd_result = sess.run( - [heatmaps, offsets, displacementFwd, displacementBwd], feed_dict={image: input_image}) - colors = [[255, 0, 0], [255, 170, 0], [255, 170, 0], [255, 255, 0], [255, 255, 0], [170, 255, 0], [170, 255, 0], [0, 255, 0], - [0, 255, 0], [0, 255, 170], [0, 255, 170], [ - 0, 170, 255], [0, 170, 255], [0, 0, 255], [0, 0, 255], - [255, 0, 255], [255, 0, 255]] - - pairs = [[5, 6], [5, 7], [6, 8], [7, 9], [8, 10], [5, 11], [ - 6, 12], [11, 12], [11, 13], [12, 14], [13, 15], [14, 16]] + ( + heatmaps_result, + offsets_result, + displacementFwd_result, + displacementBwd_result, + ) = sess.run( + [heatmaps, offsets, displacementFwd, displacementBwd], + feed_dict={image: input_image}, + ) + colors = [ + [255, 0, 0], + [255, 170, 0], + [255, 170, 0], + [255, 255, 0], + [255, 255, 0], + [170, 255, 0], + [170, 255, 0], + [0, 255, 0], + [0, 255, 0], + [0, 255, 170], + [0, 255, 170], + [0, 170, 255], + [0, 170, 255], + [0, 0, 255], + [0, 0, 255], + [255, 0, 255], + [255, 0, 255], + ] + + pairs = [ + [5, 6], + [5, 7], + [6, 8], + [7, 9], + [8, 10], + [5, 11], + [6, 12], + [11, 12], + [11, 13], + [12, 14], + [13, 15], + [14, 16], + ] keypoint = [] ckeypoint = [] @@ -88,16 +119,15 @@ def main(): ry = re[0][0] rx = re[1][0] - offsets_resulty = bbb[0+k] - offsets_resultx = bbb[17+k] - ofx = int(offsets_resultx[ry][rx]+0.5) - ofy = int(offsets_resulty[ry][rx]+0.5) + offsets_resulty = bbb[0 + k] + offsets_resultx = bbb[17 + k] + ofx = int(offsets_resultx[ry][rx] + 0.5) + ofy = int(offsets_resulty[ry][rx] + 0.5) - px = (rx)*16+ofx - py = (ry)*16+ofy + px = (rx) * 16 + ofx + py = (ry) * 16 + ofy if maxheat > 0.7: - cvs.circle(tmpimg, (int(px), int(py)), - 3, colors[k], -1) + cvs.circle(tmpimg, (int(px), int(py)), 3, colors[k], -1) keypoint.append(int(px)) keypoint.append(int(py)) else: @@ -115,18 +145,23 @@ def main(): # ckeypoint.append(keypoint[2*i+1]) # print ('chang:',i,'to',i+1) # else : - ckeypoint.append(keypoint[2*i]) - ckeypoint.append(keypoint[2*i+1]) - ckeypoint.append(keypoint[2*i+2]) - ckeypoint.append(keypoint[2*i+2+1]) + ckeypoint.append(keypoint[2 * i]) + ckeypoint.append(keypoint[2 * i + 1]) + ckeypoint.append(keypoint[2 * i + 2]) + ckeypoint.append(keypoint[2 * i + 2 + 1]) for pair in pairs: - if ckeypoint[2*pair[0]] > 0 and ckeypoint[2*pair[1]] > 0: - cvs.line(tmpimg, (ckeypoint[2*pair[0]], ckeypoint[2*pair[0]+1]), - (ckeypoint[2*pair[1]], ckeypoint[2*pair[1]+1]), (0, 0, 255), 1) + if ckeypoint[2 * pair[0]] > 0 and ckeypoint[2 * pair[1]] > 0: + cvs.line( + tmpimg, + (ckeypoint[2 * pair[0]], ckeypoint[2 * pair[0] + 1]), + (ckeypoint[2 * pair[1]], ckeypoint[2 * pair[1] + 1]), + (0, 0, 255), + 1, + ) cvs.imshow(tmpimg) - print('frames:', fcount) + print("frames:", fcount) class MyApp(App): @@ -139,18 +174,18 @@ def idle(self): def main(self): # creating a container VBox type, vertical (you can use also HBox or Widget) - main_container = gui.VBox(width=360, height=680, style={ - 'margin': '0px auto'}) + main_container = gui.VBox(width=360, height=680, style={"margin": "0px auto"}) self.aidcam = OpencvVideoWidget(self, width=340, height=480) - self.aidcam.style['margin'] = '10px' + self.aidcam.style["margin"] = "10px" self.aidcam.set_identifier("myimage_receiver") main_container.append(self.aidcam) # label show fps - self.lbl = gui.Label('This show FPS!', width=360, - height=30, left='100px', margin='10px') + self.lbl = gui.Label( + "This show FPS!", width=360, height=30, left="100px", margin="10px" + ) main_container.append(self.lbl) return main_container From fe27b5623ec56b2535ac729d99ac47a9551d2d22 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 9 Aug 2020 14:45:39 +0000 Subject: [PATCH 3/6] Restyled by isort --- gui_wizard/helloworld.py | 4 +--- src/facencnn/facepose.py | 2 +- src/handtf/utils/detector_utils.py | 11 ++++++----- src/handtf/webcam.py | 5 ++--- src/mutilpose/posenet/converter/config.py | 3 ++- src/mutilpose/posenet/decode_multi.py | 5 +++-- src/mutilpose/posenet/model.py | 3 ++- src/mutilpose/posenet/utils.py | 4 ++-- src/mutilpose/webcam_demo.py | 3 ++- src/singlepose/webpose.py | 1 + 10 files changed, 22 insertions(+), 19 deletions(-) diff --git a/gui_wizard/helloworld.py b/gui_wizard/helloworld.py index 8df59f8..d971a2a 100644 --- a/gui_wizard/helloworld.py +++ b/gui_wizard/helloworld.py @@ -1,10 +1,8 @@ # -*- coding: utf-8 -*- -from remi.gui import * - - from cvs import * +from remi.gui import * class untitled(App): diff --git a/src/facencnn/facepose.py b/src/facencnn/facepose.py index 308871e..b002295 100644 --- a/src/facencnn/facepose.py +++ b/src/facencnn/facepose.py @@ -1,6 +1,6 @@ -from cvs import * import facerecognition import numpy as np +from cvs import * def main(): diff --git a/src/handtf/utils/detector_utils.py b/src/handtf/utils/detector_utils.py index 24cc502..9ed810d 100644 --- a/src/handtf/utils/detector_utils.py +++ b/src/handtf/utils/detector_utils.py @@ -1,14 +1,15 @@ # Utilities for object detector. -import numpy as np -import sys -import tensorflow as tf import os -from threading import Thread +import sys +from collections import defaultdict from datetime import datetime +from threading import Thread + import cv2 +import numpy as np +import tensorflow as tf from utils import label_map_util -from collections import defaultdict detection_graph = tf.Graph() diff --git a/src/handtf/webcam.py b/src/handtf/webcam.py index 9db7e94..ed940ba 100644 --- a/src/handtf/webcam.py +++ b/src/handtf/webcam.py @@ -1,9 +1,8 @@ -import datetime import argparse - -from utils import detector_utils as detector_utils +import datetime from cvs import * +from utils import detector_utils as detector_utils ap = argparse.ArgumentParser() ap.add_argument( diff --git a/src/mutilpose/posenet/converter/config.py b/src/mutilpose/posenet/converter/config.py index cbf2b6e..ac4f782 100644 --- a/src/mutilpose/posenet/converter/config.py +++ b/src/mutilpose/posenet/converter/config.py @@ -1,6 +1,7 @@ -import yaml import os +import yaml + BASE_DIR = os.path.dirname(__file__) diff --git a/src/mutilpose/posenet/decode_multi.py b/src/mutilpose/posenet/decode_multi.py index 856521b..18dce5b 100644 --- a/src/mutilpose/posenet/decode_multi.py +++ b/src/mutilpose/posenet/decode_multi.py @@ -1,7 +1,8 @@ -from posenet.decode import * -from posenet.constants import * import time + import scipy.ndimage as ndi +from posenet.constants import * +from posenet.decode import * def within_nms_radius(poses, squared_nms_radius, point, keypoint_id): diff --git a/src/mutilpose/posenet/model.py b/src/mutilpose/posenet/model.py index a473680..502ef11 100644 --- a/src/mutilpose/posenet/model.py +++ b/src/mutilpose/posenet/model.py @@ -1,6 +1,7 @@ -import tensorflow as tf import os + import posenet.converter.config +import tensorflow as tf MODEL_DIR = "./_models" DEBUG_OUTPUT = False diff --git a/src/mutilpose/posenet/utils.py b/src/mutilpose/posenet/utils.py index cc41065..cc88653 100644 --- a/src/mutilpose/posenet/utils.py +++ b/src/mutilpose/posenet/utils.py @@ -1,8 +1,8 @@ +import sys + import cv2 import numpy as np - import posenet.constants -import sys scale_factor = 1.0 if sys.version_info < (3, 5): diff --git a/src/mutilpose/webcam_demo.py b/src/mutilpose/webcam_demo.py index 31d0b2e..dac2584 100644 --- a/src/mutilpose/webcam_demo.py +++ b/src/mutilpose/webcam_demo.py @@ -1,6 +1,7 @@ -import posenet import argparse import time + +import posenet import tensorflow as tf from cvs import * diff --git a/src/singlepose/webpose.py b/src/singlepose/webpose.py index a20220e..d42e4d9 100644 --- a/src/singlepose/webpose.py +++ b/src/singlepose/webpose.py @@ -1,4 +1,5 @@ import time + import numpy as np import tensorflow as tf from cvs import * From 916638818088bd4651607f54ceaf06b8301d5cdd Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 9 Aug 2020 14:45:39 +0000 Subject: [PATCH 4/6] Restyled by reorder-python-imports --- gui_wizard/helloworld.py | 2 -- src/cvs/webcam.py | 1 - src/handtf/utils/detector_utils.py | 1 - 3 files changed, 4 deletions(-) diff --git a/gui_wizard/helloworld.py b/gui_wizard/helloworld.py index d971a2a..b3da34a 100644 --- a/gui_wizard/helloworld.py +++ b/gui_wizard/helloworld.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- - - from cvs import * from remi.gui import * diff --git a/src/cvs/webcam.py b/src/cvs/webcam.py index 4bed753..116fa07 100644 --- a/src/cvs/webcam.py +++ b/src/cvs/webcam.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- - from cvs import * diff --git a/src/handtf/utils/detector_utils.py b/src/handtf/utils/detector_utils.py index 9ed810d..3d1a1cc 100644 --- a/src/handtf/utils/detector_utils.py +++ b/src/handtf/utils/detector_utils.py @@ -1,5 +1,4 @@ # Utilities for object detector. - import os import sys from collections import defaultdict From ba6fe8552b7fec93077b303ea9cc2fed40e43565 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 9 Aug 2020 14:45:40 +0000 Subject: [PATCH 5/6] Restyled by whitespace --- gui_cvs/table.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gui_cvs/table.py b/gui_cvs/table.py index 4ea58ae..dbcf76a 100755 --- a/gui_cvs/table.py +++ b/gui_cvs/table.py @@ -12,7 +12,7 @@ def main(self): lbl_description = gui.Label( """Example about TableWidget usage. - Change rows and columns count in order to see the behaviour. + Change rows and columns count in order to see the behaviour. After changing the size, 'Fill the table' content by means of the button.""" ) From e1d3bd4becc9e063430d8ad87d1b70513930e0e9 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 9 Aug 2020 14:45:41 +0000 Subject: [PATCH 6/6] Restyled by yapf --- gui_cvs/table.py | 19 +++-- gui_wizard/helloworld.py | 117 ++++++++++++-------------- src/cvs/webcam.py | 23 +++-- src/facencnn/facepose.py | 11 ++- src/handtf/utils/detector_utils.py | 30 +++---- src/handtf/webcam.py | 19 +++-- src/mutilpose/posenet/decode_multi.py | 87 +++++++++---------- src/mutilpose/posenet/model.py | 6 +- src/mutilpose/posenet/utils.py | 94 ++++++++++----------- src/mutilpose/webcam_demo.py | 15 ++-- src/singlepose/webpose.py | 30 ++++--- 11 files changed, 231 insertions(+), 220 deletions(-) diff --git a/gui_cvs/table.py b/gui_cvs/table.py index dbcf76a..6d0c91f 100755 --- a/gui_cvs/table.py +++ b/gui_cvs/table.py @@ -6,15 +6,17 @@ def __init__(self, *args): super(MyApp, self).__init__(*args) def main(self): - wid = gui.VBox( - width=500, height=500, style={"margin": "5px auto", "padding": "10px"} - ) - - lbl_description = gui.Label( - """Example about TableWidget usage. + wid = gui.VBox(width=500, + height=500, + style={ + "margin": "5px auto", + "padding": "10px" + }) + + lbl_description = gui.Label("""Example about TableWidget usage. Change rows and columns count in order to see the behaviour. After changing the size, 'Fill the table' content by means of the button.""" - ) + ) wid.append(lbl_description) @@ -62,7 +64,8 @@ def on_column_count_change(self, emitter, value, table): def fill_table(self, emitter, table): for ri in range(table.row_count()): for ci in range(table.column_count()): - table.item_at(ri, ci).set_text("row:%s,column:%s" % (str(ri), str(ci))) + table.item_at(ri, ci).set_text("row:%s,column:%s" % + (str(ri), str(ci))) def on_use_title_change(self, emitter, value, table): value = value == "true" diff --git a/gui_wizard/helloworld.py b/gui_wizard/helloworld.py index b3da34a..82ed273 100644 --- a/gui_wizard/helloworld.py +++ b/gui_wizard/helloworld.py @@ -7,7 +7,8 @@ class untitled(App): def __init__(self, *args, **kwargs): # DON'T MAKE CHANGES HERE, THIS METHOD GETS OVERWRITTEN WHEN SAVING IN THE EDITOR if "editing_mode" not in kwargs.keys(): - super(untitled, self).__init__(*args, static_file_path={"my_res": "./res/"}) + super(untitled, + self).__init__(*args, static_file_path={"my_res": "./res/"}) def idle(self): # idle function called every update cycle @@ -20,72 +21,60 @@ def main(self): def construct_ui(self): # DON'T MAKE CHANGES HERE, THIS METHOD GETS OVERWRITTEN WHEN SAVING IN THE EDITOR mainContainer = Widget() - mainContainer.attributes.update( - { - "editor_baseclass": "Widget", - "editor_tag_type": "widget", - "editor_newclass": "False", - "editor_constructor": "()", - "class": "Widget", - "editor_varname": "mainContainer", - } - ) - mainContainer.style.update( - { - "width": "266px", - "position": "absolute", - "top": "61px", - "left": "16px", - "margin": "0px", - "overflow": "auto", - "height": "257px", - } - ) + mainContainer.attributes.update({ + "editor_baseclass": "Widget", + "editor_tag_type": "widget", + "editor_newclass": "False", + "editor_constructor": "()", + "class": "Widget", + "editor_varname": "mainContainer", + }) + mainContainer.style.update({ + "width": "266px", + "position": "absolute", + "top": "61px", + "left": "16px", + "margin": "0px", + "overflow": "auto", + "height": "257px", + }) button = Button("Say Hello") - button.attributes.update( - { - "editor_baseclass": "Button", - "editor_tag_type": "widget", - "editor_newclass": "False", - "editor_constructor": "('Say Hello')", - "class": "Button", - "editor_varname": "button", - } - ) - button.style.update( - { - "width": "149px", - "position": "absolute", - "top": "80px", - "left": "37px", - "margin": "0px", - "overflow": "auto", - "height": "29px", - } - ) + button.attributes.update({ + "editor_baseclass": "Button", + "editor_tag_type": "widget", + "editor_newclass": "False", + "editor_constructor": "('Say Hello')", + "class": "Button", + "editor_varname": "button", + }) + button.style.update({ + "width": "149px", + "position": "absolute", + "top": "80px", + "left": "37px", + "margin": "0px", + "overflow": "auto", + "height": "29px", + }) mainContainer.append(button, "button") label = Label("My label") - label.attributes.update( - { - "editor_baseclass": "Label", - "editor_tag_type": "widget", - "editor_newclass": "False", - "editor_constructor": "('My label')", - "class": "Label", - "editor_varname": "label", - } - ) - label.style.update( - { - "width": "98px", - "position": "absolute", - "top": "20px", - "left": "20px", - "margin": "0px", - "overflow": "auto", - "height": "25px", - } - ) + label.attributes.update({ + "editor_baseclass": "Label", + "editor_tag_type": "widget", + "editor_newclass": "False", + "editor_constructor": "('My label')", + "class": "Label", + "editor_varname": "label", + }) + label.style.update({ + "width": "98px", + "position": "absolute", + "top": "20px", + "left": "20px", + "margin": "0px", + "overflow": "auto", + "height": "25px", + }) mainContainer.append(label, "label") mainContainer.children["button"].onclick.do(self.onclick_button) diff --git a/src/cvs/webcam.py b/src/cvs/webcam.py index 116fa07..11a2858 100644 --- a/src/cvs/webcam.py +++ b/src/cvs/webcam.py @@ -6,7 +6,8 @@ class untitled(App): def __init__(self, *args, **kwargs): # DON'T MAKE CHANGES HERE, THIS METHOD GETS OVERWRITTEN WHEN SAVING IN THE EDITOR if "editing_mode" not in kwargs.keys(): - super(untitled, self).__init__(*args, static_file_path={"my_res": "./res/"}) + super(untitled, + self).__init__(*args, static_file_path={"my_res": "./res/"}) def idle(self): # idle function called every update cycle @@ -21,17 +22,15 @@ def construct_ui(self): aidcam = OpencvVideoWidget(self) # aidcam.attributes.update({"src":"/528175722896/get_image_data","editor_newclass":"False","editor_baseclass":"OpencvVideoWidget","editor_constructor":"()","class":"OpencvVideoWidget","editor_tag_type":"widget","editor_varname":"aidcam"}) aidcam.set_identifier("myimage_receiver") - aidcam.style.update( - { - "width": "190px", - "position": "absolute", - "top": "20px", - "left": "20px", - "margin": "0px", - "overflow": "auto", - "height": "177px", - } - ) + aidcam.style.update({ + "width": "190px", + "position": "absolute", + "top": "20px", + "left": "20px", + "margin": "0px", + "overflow": "auto", + "height": "177px", + }) self.aidcam = aidcam return self.aidcam diff --git a/src/facencnn/facepose.py b/src/facencnn/facepose.py index b002295..c4a75aa 100644 --- a/src/facencnn/facepose.py +++ b/src/facencnn/facepose.py @@ -68,14 +68,21 @@ def idle(self): def main(self): # creating a container VBox type, vertical (you can use also HBox or Widget) - main_container = gui.VBox(width=360, height=680, style={"margin": "0px auto"}) + main_container = gui.VBox(width=360, + height=680, + style={"margin": "0px auto"}) self.aidcam = OpencvVideoWidget(self, width=340, height=480) self.aidcam.style["margin"] = "10px" self.aidcam.set_identifier("myimage_receiver") main_container.append(self.aidcam) - self.lbl = gui.Label("This show fps!", width=360, height=30, margin="50px",) + self.lbl = gui.Label( + "This show fps!", + width=360, + height=30, + margin="50px", + ) main_container.append(self.lbl) return main_container diff --git a/src/handtf/utils/detector_utils.py b/src/handtf/utils/detector_utils.py index 3d1a1cc..e359b1a 100644 --- a/src/handtf/utils/detector_utils.py +++ b/src/handtf/utils/detector_utils.py @@ -22,8 +22,7 @@ # load label map using utils provided by tensorflow object detection api label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( - label_map, max_num_classes=NUM_CLASSES, use_display_name=True -) + label_map, max_num_classes=NUM_CLASSES, use_display_name=True) category_index = label_map_util.create_category_index(categories) @@ -54,14 +53,14 @@ def load_inference_graph(): # Drawing bounding boxes and distances onto image def draw_box_on_image( - num_hands_detect, - score_thresh, - scores, - boxes, - classes, - im_width, - im_height, - image_np, + num_hands_detect, + score_thresh, + scores, + boxes, + classes, + im_width, + im_height, + image_np, ): # Determined using a piece of paper of known length, code can be found in distance to camera focalLength = 875 @@ -90,7 +89,8 @@ def draw_box_on_image( p1 = (int(left), int(top)) p2 = (int(right), int(bottom)) - dist = distance_to_camera(avg_width, focalLength, int(right - left)) + dist = distance_to_camera(avg_width, focalLength, + int(right - left)) cv2.rectangle(image_np, p1, p2, color, 3, 1) @@ -127,9 +127,8 @@ def draw_box_on_image( # Show fps value on image. def draw_text_on_image(fps, image_np): - cv2.putText( - image_np, fps, (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (77, 255, 9), 2 - ) + cv2.putText(image_np, fps, (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, + (77, 255, 9), 2) # compute and return the distance from the hand to the camera using triangle similarity @@ -150,7 +149,8 @@ def detect_objects(image_np, detection_graph, sess): # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. detection_scores = detection_graph.get_tensor_by_name("detection_scores:0") - detection_classes = detection_graph.get_tensor_by_name("detection_classes:0") + detection_classes = detection_graph.get_tensor_by_name( + "detection_classes:0") num_detections = detection_graph.get_tensor_by_name("num_detections:0") image_np_expanded = np.expand_dims(image_np, axis=0) diff --git a/src/handtf/webcam.py b/src/handtf/webcam.py index ed940ba..d1f919f 100644 --- a/src/handtf/webcam.py +++ b/src/handtf/webcam.py @@ -15,7 +15,6 @@ ) args = vars(ap.parse_args()) - detection_graph, sess = detector_utils.load_inference_graph() @@ -54,8 +53,7 @@ def main(): # Run image through tensorflow graph boxes, scores, classes = detector_utils.detect_objects( - frame, detection_graph, sess - ) + frame, detection_graph, sess) # Draw bounding boxeses and text detector_utils.draw_box_on_image( @@ -71,7 +69,8 @@ def main(): # Calculate Frames per second (FPS) num_frames += 1 - elapsed_time = (datetime.datetime.now() - start_time).total_seconds() + elapsed_time = (datetime.datetime.now() - + start_time).total_seconds() fps = num_frames / elapsed_time # Display FPS on frame @@ -81,8 +80,7 @@ def main(): if args["display"]: detector_utils.draw_text_on_image( - "FPS : " + str("{0:.2f}".format(fps)), frame - ) + "FPS : " + str("{0:.2f}".format(fps)), frame) cvs.imshow(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)) @@ -103,7 +101,9 @@ def idle(self): def main(self): # initcv(process) # creating a container VBox type, vertical (you can use also HBox or Widget) - main_container = gui.VBox(width=360, height=680, style={"margin": "0px auto"}) + main_container = gui.VBox(width=360, + height=680, + style={"margin": "0px auto"}) self.aidcam = OpencvVideoWidget(self, width=340, height=480) self.aidcam.style["margin"] = "10px" @@ -112,7 +112,10 @@ def main(self): main_container.append(self.aidcam) # Display FPS on frame - self.lbl = gui.Label("This is a LABEL!", width=360, height=30, margin="10px") + self.lbl = gui.Label("This is a LABEL!", + width=360, + height=30, + margin="10px") main_container.append(self.lbl) return main_container diff --git a/src/mutilpose/posenet/decode_multi.py b/src/mutilpose/posenet/decode_multi.py index 18dce5b..40970b5 100644 --- a/src/mutilpose/posenet/decode_multi.py +++ b/src/mutilpose/posenet/decode_multi.py @@ -7,50 +7,45 @@ def within_nms_radius(poses, squared_nms_radius, point, keypoint_id): return any( - np.sum((pose_coord[keypoint_id] - point) ** 2) <= squared_nms_radius - for _, _, pose_coord in poses - ) + np.sum((pose_coord[keypoint_id] - point)**2) <= squared_nms_radius + for _, _, pose_coord in poses) def within_nms_radius_fast(pose_coords, squared_nms_radius, point): if not pose_coords.shape[0]: return False - return np.any(np.sum((pose_coords - point) ** 2, axis=1) <= squared_nms_radius) + return np.any( + np.sum((pose_coords - point)**2, axis=1) <= squared_nms_radius) -def get_instance_score( - existing_poses, squared_nms_radius, keypoint_scores, keypoint_coords -): +def get_instance_score(existing_poses, squared_nms_radius, keypoint_scores, + keypoint_coords): not_overlapped_scores = 0.0 for keypoint_id in range(len(keypoint_scores)): if not within_nms_radius( - existing_poses, - squared_nms_radius, - keypoint_coords[keypoint_id], - keypoint_id, + existing_poses, + squared_nms_radius, + keypoint_coords[keypoint_id], + keypoint_id, ): not_overlapped_scores += keypoint_scores[keypoint_id] return not_overlapped_scores / len(keypoint_scores) -def get_instance_score_fast( - exist_pose_coords, squared_nms_radius, keypoint_scores, keypoint_coords -): +def get_instance_score_fast(exist_pose_coords, squared_nms_radius, + keypoint_scores, keypoint_coords): if exist_pose_coords.shape[0]: - s = ( - np.sum((exist_pose_coords - keypoint_coords) ** 2, axis=2) - > squared_nms_radius - ) + s = (np.sum((exist_pose_coords - keypoint_coords)**2, axis=2) > + squared_nms_radius) not_overlapped_scores = np.sum(keypoint_scores[np.all(s, axis=0)]) else: not_overlapped_scores = np.sum(keypoint_scores) return not_overlapped_scores / len(keypoint_scores) -def score_is_max_in_local_window( - keypoint_id, score, hmy, hmx, local_max_radius, scores -): +def score_is_max_in_local_window(keypoint_id, score, hmy, hmx, + local_max_radius, scores): height = scores.shape[0] width = scores.shape[1] @@ -78,9 +73,8 @@ def build_part_with_score(score_threshold, local_max_radius, scores): score = scores[hmy, hmx, keypoint_id] if score < score_threshold: continue - if score_is_max_in_local_window( - keypoint_id, score, hmy, hmx, local_max_radius, scores - ): + if score_is_max_in_local_window(keypoint_id, score, hmy, hmx, + local_max_radius, scores): parts.append((score, keypoint_id, np.array((hmy, hmx)))) return parts @@ -99,21 +93,22 @@ def build_part_with_score_fast(score_threshold, local_max_radius, scores): max_loc = np.logical_and(kp_scores == max_vals, kp_scores > 0) max_loc_idx = max_loc.nonzero() for y, x in zip(*max_loc_idx): - parts.append((scores[y, x, keypoint_id], keypoint_id, np.array((y, x)))) + parts.append( + (scores[y, x, keypoint_id], keypoint_id, np.array((y, x)))) return parts def decode_multiple_poses( - scores, - offsets, - displacements_fwd, - displacements_bwd, - output_stride, - max_pose_detections=10, - score_threshold=0.5, - nms_radius=20, - min_pose_score=0.5, + scores, + offsets, + displacements_fwd, + displacements_bwd, + output_stride, + max_pose_detections=10, + score_threshold=0.5, + nms_radius=20, + min_pose_score=0.5, ): pose_count = 0 @@ -121,29 +116,29 @@ def decode_multiple_poses( pose_keypoint_scores = np.zeros((max_pose_detections, NUM_KEYPOINTS)) pose_keypoint_coords = np.zeros((max_pose_detections, NUM_KEYPOINTS, 2)) - squared_nms_radius = nms_radius ** 2 + squared_nms_radius = nms_radius**2 - scored_parts = build_part_with_score_fast( - score_threshold, LOCAL_MAXIMUM_RADIUS, scores - ) + scored_parts = build_part_with_score_fast(score_threshold, + LOCAL_MAXIMUM_RADIUS, scores) scored_parts = sorted(scored_parts, key=lambda x: x[0], reverse=True) # change dimensions from (h, w, x) to (h, w, x//2, 2) to allow return of complete coord array height = scores.shape[0] width = scores.shape[1] offsets = offsets.reshape(height, width, 2, -1).swapaxes(2, 3) - displacements_fwd = displacements_fwd.reshape(height, width, 2, -1).swapaxes(2, 3) - displacements_bwd = displacements_bwd.reshape(height, width, 2, -1).swapaxes(2, 3) + displacements_fwd = displacements_fwd.reshape(height, width, 2, + -1).swapaxes(2, 3) + displacements_bwd = displacements_bwd.reshape(height, width, 2, + -1).swapaxes(2, 3) for root_score, root_id, root_coord in scored_parts: - root_image_coords = ( - root_coord * output_stride + offsets[root_coord[0], root_coord[1], root_id] - ) + root_image_coords = (root_coord * output_stride + + offsets[root_coord[0], root_coord[1], root_id]) if within_nms_radius_fast( - pose_keypoint_coords[:pose_count, root_id, :], - squared_nms_radius, - root_image_coords, + pose_keypoint_coords[:pose_count, root_id, :], + squared_nms_radius, + root_image_coords, ): continue diff --git a/src/mutilpose/posenet/model.py b/src/mutilpose/posenet/model.py index 502ef11..a58f6cc 100644 --- a/src/mutilpose/posenet/model.py +++ b/src/mutilpose/posenet/model.py @@ -35,9 +35,11 @@ def load_config(model_ord): def load_model(model_id, sess, model_dir=MODEL_DIR): model_ord = model_id_to_ord(model_id) model_cfg = load_config(model_ord) - model_path = os.path.join(model_dir, "model-%s.pb" % model_cfg["checkpoint_name"]) + model_path = os.path.join(model_dir, + "model-%s.pb" % model_cfg["checkpoint_name"]) if not os.path.exists(model_path): - print("Cannot find model file %s, converting from tfjs..." % model_path) + print("Cannot find model file %s, converting from tfjs..." % + model_path) from posenet.converter.tfjs2python import convert convert(model_ord, model_dir, check=False) diff --git a/src/mutilpose/posenet/utils.py b/src/mutilpose/posenet/utils.py index cc88653..52d1572 100644 --- a/src/mutilpose/posenet/utils.py +++ b/src/mutilpose/posenet/utils.py @@ -21,13 +21,12 @@ def _process_input(source_img, scale_factor=1.0, output_stride=16): source_img.shape[0] * scale_factor, output_stride=output_stride, ) - scale = np.array( - [source_img.shape[0] / target_height, source_img.shape[1] / target_width] - ) + scale = np.array([ + source_img.shape[0] / target_height, source_img.shape[1] / target_width + ]) - input_img = cv2.resize( - source_img, (target_width, target_height), interpolation=cv2.INTER_LINEAR - ) + input_img = cv2.resize(source_img, (target_width, target_height), + interpolation=cv2.INTER_LINEAR) input_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB).astype(np.float32) input_img = input_img * (2.0 / 255.0) - 1.0 input_img = input_img.reshape(1, target_height, target_width, 3) @@ -47,12 +46,12 @@ def read_imgfile(path, scale_factor=1.0, output_stride=16): def draw_keypoints( - img, - instance_scores, - keypoint_scores, - keypoint_coords, - min_pose_confidence=0.5, - min_part_confidence=0.5, + img, + instance_scores, + keypoint_scores, + keypoint_coords, + min_pose_confidence=0.5, + min_part_confidence=0.5, ): cv_keypoints = [] for ii, score in enumerate(instance_scores): @@ -65,55 +64,53 @@ def draw_keypoints( return cv2.drawKeypoints(img, cv_keypoints, outImage=np.array([])) -def get_adjacent_keypoints(keypoint_scores, keypoint_coords, min_confidence=0.1): +def get_adjacent_keypoints(keypoint_scores, + keypoint_coords, + min_confidence=0.1): results = [] for left, right in posenet.CONNECTED_PART_INDICES: - if ( - keypoint_scores[left] < min_confidence - or keypoint_scores[right] < min_confidence - ): + if (keypoint_scores[left] < min_confidence + or keypoint_scores[right] < min_confidence): continue results.append( - np.array( - [ - keypoint_coords[left][::-1] / scale_factor, - keypoint_coords[right][::-1] / scale_factor, - ] - ).astype(np.int32), - ) + np.array([ + keypoint_coords[left][::-1] / scale_factor, + keypoint_coords[right][::-1] / scale_factor, + ]).astype(np.int32), ) return results def draw_skeleton( - img, - instance_scores, - keypoint_scores, - keypoint_coords, - min_pose_confidence=0.5, - min_part_confidence=0.5, + img, + instance_scores, + keypoint_scores, + keypoint_coords, + min_pose_confidence=0.5, + min_part_confidence=0.5, ): out_img = img adjacent_keypoints = [] for ii, score in enumerate(instance_scores): if score < min_pose_confidence: continue - new_keypoints = get_adjacent_keypoints( - keypoint_scores[ii, :], keypoint_coords[ii, :, :], min_part_confidence - ) + new_keypoints = get_adjacent_keypoints(keypoint_scores[ii, :], + keypoint_coords[ii, :, :], + min_part_confidence) adjacent_keypoints.extend(new_keypoints) - out_img = cv2.polylines( - out_img, adjacent_keypoints, isClosed=False, color=(255, 255, 0) - ) + out_img = cv2.polylines(out_img, + adjacent_keypoints, + isClosed=False, + color=(255, 255, 0)) return out_img def draw_skel_and_kp( - img, - instance_scores, - keypoint_scores, - keypoint_coords, - min_pose_score=0.3, - min_part_score=0.3, + img, + instance_scores, + keypoint_scores, + keypoint_coords, + min_pose_score=0.3, + min_part_score=0.3, ): out_img = img adjacent_keypoints = [] @@ -122,22 +119,23 @@ def draw_skel_and_kp( if score < min_pose_score: continue - new_keypoints = get_adjacent_keypoints( - keypoint_scores[ii, :], keypoint_coords[ii, :, :], min_part_score - ) + new_keypoints = get_adjacent_keypoints(keypoint_scores[ii, :], + keypoint_coords[ii, :, :], + min_part_score) adjacent_keypoints.extend(new_keypoints) for ks, kc in zip(keypoint_scores[ii, :], keypoint_coords[ii, :, :]): if ks < min_part_score: continue cv_keypoints.append( - cv2.KeyPoint(kc[1] / scale_factor, kc[0] / scale_factor, 10.0 * ks) - ) + cv2.KeyPoint(kc[1] / scale_factor, kc[0] / scale_factor, + 10.0 * ks)) # out_img = cv2.drawKeypoints( # out_img, cv_keypoints, np.array([]), (0, 0, 255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # return out_img - out_img2 = cv2.polylines(out_img, adjacent_keypoints, False, (255, 255, 0), 2) + out_img2 = cv2.polylines(out_img, adjacent_keypoints, False, (255, 255, 0), + 2) # out_img1 = cv2.drawKeypoints( # out_img, cv_keypoints, np.array([]), (0, 0, 255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) if out_img2 is None: diff --git a/src/mutilpose/webcam_demo.py b/src/mutilpose/webcam_demo.py index dac2584..8028781 100644 --- a/src/mutilpose/webcam_demo.py +++ b/src/mutilpose/webcam_demo.py @@ -7,7 +7,6 @@ print("import tensorflow...wait...") - parser = argparse.ArgumentParser() parser.add_argument("--model", type=int, default=50) parser.add_argument("--cam", type=int, default=0) @@ -40,8 +39,9 @@ def main(): img = cvs.flip(img, 0) input_image, display_image, output_scale = posenet.read_cap( - img, scale_factor=args.scale_factor, output_stride=output_stride - ) + img, + scale_factor=args.scale_factor, + output_stride=output_stride) ( heatmaps_result, @@ -94,7 +94,9 @@ def idle(self): def main(self): # creating a container VBox type, vertical (you can use also HBox or Widget) - main_container = gui.VBox(width=360, height=680, style={"margin": "0px auto"}) + main_container = gui.VBox(width=360, + height=680, + style={"margin": "0px auto"}) self.aidcam = OpencvVideoWidget(self, width=340, height=480) self.aidcam.style["margin"] = "10px" @@ -103,7 +105,10 @@ def main(self): # The post request must specify in the header the widget identifier and the function name that will be used to pass the file data self.aidcam.set_identifier("myimage_receiver") main_container.append(self.aidcam) - self.lbl = gui.Label("This show FPS!", width=360, height=30, margin="10px") + self.lbl = gui.Label("This show FPS!", + width=360, + height=30, + margin="10px") main_container.append(self.lbl) return main_container diff --git a/src/singlepose/webpose.py b/src/singlepose/webpose.py index d42e4d9..f88a630 100644 --- a/src/singlepose/webpose.py +++ b/src/singlepose/webpose.py @@ -39,8 +39,10 @@ def main(): image = detection_graph.get_tensor_by_name("image:0") heatmaps = detection_graph.get_tensor_by_name("heatmap:0") offsets = detection_graph.get_tensor_by_name("offset_2:0") - displacementFwd = detection_graph.get_tensor_by_name("displacement_fwd_2:0") - displacementBwd = detection_graph.get_tensor_by_name("displacement_bwd_2:0") + displacementFwd = detection_graph.get_tensor_by_name( + "displacement_fwd_2:0") + displacementBwd = detection_graph.get_tensor_by_name( + "displacement_bwd_2:0") fcount = -1 start = time.time() @@ -128,7 +130,8 @@ def main(): px = (rx) * 16 + ofx py = (ry) * 16 + ofy if maxheat > 0.7: - cvs.circle(tmpimg, (int(px), int(py)), 3, colors[k], -1) + cvs.circle(tmpimg, (int(px), int(py)), 3, colors[k], + -1) keypoint.append(int(px)) keypoint.append(int(py)) else: @@ -152,11 +155,14 @@ def main(): ckeypoint.append(keypoint[2 * i + 2 + 1]) for pair in pairs: - if ckeypoint[2 * pair[0]] > 0 and ckeypoint[2 * pair[1]] > 0: + if ckeypoint[2 * pair[0]] > 0 and ckeypoint[2 * + pair[1]] > 0: cvs.line( tmpimg, - (ckeypoint[2 * pair[0]], ckeypoint[2 * pair[0] + 1]), - (ckeypoint[2 * pair[1]], ckeypoint[2 * pair[1] + 1]), + (ckeypoint[2 * pair[0]], + ckeypoint[2 * pair[0] + 1]), + (ckeypoint[2 * pair[1]], + ckeypoint[2 * pair[1] + 1]), (0, 0, 255), 1, ) @@ -175,7 +181,9 @@ def idle(self): def main(self): # creating a container VBox type, vertical (you can use also HBox or Widget) - main_container = gui.VBox(width=360, height=680, style={"margin": "0px auto"}) + main_container = gui.VBox(width=360, + height=680, + style={"margin": "0px auto"}) self.aidcam = OpencvVideoWidget(self, width=340, height=480) self.aidcam.style["margin"] = "10px" @@ -184,9 +192,11 @@ def main(self): main_container.append(self.aidcam) # label show fps - self.lbl = gui.Label( - "This show FPS!", width=360, height=30, left="100px", margin="10px" - ) + self.lbl = gui.Label("This show FPS!", + width=360, + height=30, + left="100px", + margin="10px") main_container.append(self.lbl) return main_container