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..6d0c91f 100755 --- a/gui_cvs/table.py +++ b/gui_cvs/table.py @@ -1,41 +1,49 @@ 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. - After changing the size, 'Fill the table' content by means of the button.""") + 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) 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) @@ -56,17 +64,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' + 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..82ed273 100644 --- a/gui_wizard/helloworld.py +++ b/gui_wizard/helloworld.py @@ -1,69 +1,107 @@ - # -*- coding: utf-8 -*- - - -from remi.gui import * - - from cvs import * +from remi.gui import * 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/'}) + # 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/"}) 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"}) - 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') - + 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..11a2858 100644 --- a/src/cvs/webcam.py +++ b/src/cvs/webcam.py @@ -1,50 +1,58 @@ - # -*- coding: utf-8 -*- - from cvs import * 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/'}) + # 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/"}) 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..c4a75aa 100644 --- a/src/facencnn/facepose.py +++ b/src/facencnn/facepose.py @@ -1,59 +1,59 @@ -from cvs import * import facerecognition import numpy as np +from cvs import * 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') - #cvs.infoshow('draw bounding box for the face') - rect = ret['rect'] - #print rect - mtcnn = ret['mtcnn'] - #print mtcnn + # for ret in each: + print("draw bounding box for the face") + # cvs.infoshow('draw bounding box for the face') + rect = ret["rect"] + # print rect + 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'] - #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])) + 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) + # 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]) - 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,34 @@ 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.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 6b546e6..e359b1a 100644 --- a/src/handtf/utils/detector_utils.py +++ b/src/handtf/utils/detector_utils.py @@ -1,22 +1,22 @@ # 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() -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 @@ -34,23 +34,34 @@ 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='') - - 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) + 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, + ) + + sess = tf.Session(config=config, graph=detection_graph) print("> ====== Inference graph loaded.") return detection_graph, sess # 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 @@ -58,65 +69,94 @@ 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 scores[i] > score_thresh: + 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, - 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.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) +# 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, '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) +# compute and return the distance from the hand to the camera using triangle similarity -# 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) -# 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_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_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 b6509c1..d1f919f 100644 --- a/src/handtf/webcam.py +++ b/src/handtf/webcam.py @@ -1,16 +1,20 @@ -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('-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()) - detection_graph, sess = detector_utils.load_inference_graph() @@ -18,7 +22,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 +37,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,10 +55,17 @@ 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) + num_hands_detect, + score_thresh, + scores, + boxes, + classes, + im_width, + im_height, + frame, + ) # Calculate Frames per second (FPS) num_frames += 1 @@ -66,48 +77,50 @@ def main(): 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) + 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.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..ac4f782 100644 --- a/src/mutilpose/posenet/converter/config.py +++ b/src/mutilpose/posenet/converter/config.py @@ -1,9 +1,10 @@ -import yaml import os +import yaml + 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 87a92da..40970b5 100644 --- a/src/mutilpose/posenet/decode_multi.py +++ b/src/mutilpose/posenet/decode_multi.py @@ -1,48 +1,51 @@ -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): 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): - not_overlapped_scores = 0. +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] @@ -72,9 +75,7 @@ def build_part_with_score(score_threshold, local_max_radius, scores): 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)) - )) + parts.append((score, keypoint_id, np.array((hmy, hmx)))) return parts @@ -87,62 +88,83 @@ 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) 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 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 da992a4..a58f6cc 100644 --- a/src/mutilpose/posenet/model.py +++ b/src/mutilpose/posenet/model.py @@ -1,8 +1,9 @@ -import tensorflow as tf import os + import posenet.converter.config +import tensorflow as tf -MODEL_DIR = './_models' +MODEL_DIR = "./_models" DEBUG_OUTPUT = False @@ -21,42 +22,45 @@ 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 fdc6631..52d1572 100644 --- a/src/mutilpose/posenet/utils.py +++ b/src/mutilpose/posenet/utils.py @@ -1,11 +1,13 @@ +import sys + import cv2 import numpy as np - 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 @@ -15,10 +17,16 @@ 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]) - - input_img = cv2.resize(source_img, (target_width, target_height), interpolation=cv2.INTER_LINEAR) + 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) 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) @@ -26,8 +34,8 @@ 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: + # res, img = cap.read() + # if not res: # raise IOError("webcam failure") return _process_input(img, scale_factor, output_stride) @@ -38,8 +46,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: @@ -47,39 +60,58 @@ 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): +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 = [] cv_keypoints = [] @@ -87,25 +119,39 @@ 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. * ks)) + cv_keypoints.append( + cv2.KeyPoint(kc[1] / scale_factor, kc[0] / scale_factor, + 10.0 * 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( - # out_img, cv_keypoints, np.array([]), (0, 0, 255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) - if out_img2 is None : + # 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: 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..8028781 100644 --- a/src/mutilpose/webcam_demo.py +++ b/src/mutilpose/webcam_demo.py @@ -1,111 +1,120 @@ - -from cvs import * - -print ('import tensorflow...wait...') -import tensorflow as tf -import time import argparse +import time import posenet +import tensorflow as tf +from cvs import * - - +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 +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'] - + 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) - - 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( + 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}) + + ( + 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 + # 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) - + 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 + 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 - if __name__ == "__main__": - + initcv(main) startcv(MyApp) diff --git a/src/singlepose/webpose.py b/src/singlepose/webpose.py index 49d0294..f88a630 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 @@ -15,98 +15,133 @@ 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 + 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 + 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() 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]) @@ -114,44 +149,57 @@ 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]) - - 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) + 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, + ) 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.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__":