# -*- coding: utf-8 -*- """ Created on Thu Oct 3 20:41:35 2019 @author: ASUS """ import cv2 import numpy as np import settings from pose.estimator import TfPoseEstimator poseEstimator = None cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() show = cv2.resize(frame, (settings.winWidth, settings.winHeight)) show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB) humans = poseEstimator.inference(show) print(humans) show = TfPoseEstimator.draw_humans(show, humans, imgcopy=False)
def show_camera(self): start = time.time() ret, frame = self.cap.read() # if not ret: # print('this should save the video') # self.timer_camera.stop() # self.out.release() if ret: show_s = cv2.resize(frame, (settings.winWidth, settings.winHeight)) show = cv2.cvtColor(show_s, cv2.COLOR_BGR2RGB) if self.__flag_mode == 1: self.infoBox.setText(u'当前为人体Attitude Estimation模式') humans = poseEstimator.inference(show) show = TfPoseEstimator.draw_humans(show, humans, imgcopy=False) elif self.__flag_mode == 2: self.infoBox.setText(u'当前为Multiplayer tracking模式') humans = poseEstimator.inference(show) show, joints, bboxes, xcenter, sk = TfPoseEstimator.get_skeleton( show, humans, imgcopy=False) show = TfPoseEstimator.draw_humans(show, humans, imgcopy=False) height = show.shape[0] width = show.shape[1] if bboxes: result = np.array(bboxes) det = result[:, 0:5] det[:, 0] = det[:, 0] * width det[:, 1] = det[:, 1] * height det[:, 2] = det[:, 2] * width det[:, 3] = det[:, 3] * height trackers = self.tracker.update(det) for d in trackers: xmin = int(d[0]) ymin = int(d[1]) xmax = int(d[2]) ymax = int(d[3]) label = int(d[4]) cv2.rectangle(show, (xmin, ymin), (xmax, ymax), (int(settings.c[label % 32, 0]), int(settings.c[label % 32, 1]), int(settings.c[label % 32, 2])), 4) # This is the part to get the kt points elif self.__flag_mode == 3: self.infoBox.setText(u'当前为人体Behavior recognition模式') humans = poseEstimator.inference(show) ori = np.copy(show) show, joints, bboxes, xcenter, sk = TfPoseEstimator.get_skeleton( show, humans, imgcopy=False) show = TfPoseEstimator.draw_humans(show, humans, imgcopy=False) height = show.shape[0] width = show.shape[1] if bboxes: result = np.array(bboxes) det = result[:, 0:5] det[:, 0] = det[:, 0] * width det[:, 1] = det[:, 1] * height det[:, 2] = det[:, 2] * width det[:, 3] = det[:, 3] * height trackers = self.tracker.update(det) self.current = [i[-1] for i in trackers] if len(self.previous) > 0: for item in self.previous: if item not in self.current and item in self.data: del self.data[item] if item not in self.current and item in self.memory: del self.memory[item] self.previous = self.current for d in trackers: xmin = int(d[0]) ymin = int(d[1]) xmax = int(d[2]) ymax = int(d[3]) label = int(d[4]) try: j = np.argmin( np.array([ abs(i - (xmax + xmin) / 2.) for i in xcenter ])) except: j = 0 if joint_filter(joints[j]): joints[j] = joint_completion( joint_completion(joints[j])) if label not in self.data: self.data[label] = [joints[j]] self.memory[label] = 0 else: self.data[label].append(joints[j]) if len(self.data[label]) == settings.L: pred = actionPredictor().move_status( self.data[label]) if pred == 0: pred = self.memory[label] else: self.memory[label] = pred self.data[label].pop(0) location = self.data[label][-1][1] if location[0] <= 30: location = (51, location[1]) if location[1] <= 10: location = (location[0], 31) cv2.putText( show, settings.move_status[pred], (location[0] - 30, location[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) cv2.rectangle(show, (xmin, ymin), (xmax, ymax), (int(settings.c[label % 32, 0]), int(settings.c[label % 32, 1]), int(settings.c[label % 32, 2])), 4) self.out.write(show_s) # Write out frame to video end = time.time() self.fps = 1. / (end - start) cv2.putText(show, 'FPS: %.2f' % self.fps, (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888) self.label_show_camera.setPixmap( QtGui.QPixmap.fromImage(showImage)) else: print('this should save the video') self.timer_camera.stop() self.out.release()
def show_camera(self): start = time.time() ret, frame = self.cap.read() show = cv2.resize(frame, (settings.winWidth, settings.winHeight)) show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB) if ret: if self.__flag_mode == 1: self.infoBox.setText(u'当前为人体姿态估计模式') humans = poseEstimator.inference(show) show = TfPoseEstimator.draw_humans(show, humans, imgcopy=False) elif self.__flag_mode == 2: self.infoBox.setText(u'当前为多人跟踪模式') humans = poseEstimator.inference(show) show, joints, bboxes, xcenter = TfPoseEstimator.get_skeleton( show, humans, imgcopy=False) height = show.shape[0] width = show.shape[1] if bboxes: result = np.array(bboxes) det = result[:, 0:5] det[:, 0] = det[:, 0] * width det[:, 1] = det[:, 1] * height det[:, 2] = det[:, 2] * width det[:, 3] = det[:, 3] * height trackers = self.tracker.update(det) for d in trackers: xmin = int(d[0]) ymin = int(d[1]) xmax = int(d[2]) ymax = int(d[3]) label = int(d[4]) cv2.rectangle(show, (xmin, ymin), (xmax, ymax), (int(settings.c[label % 32, 0]), int(settings.c[label % 32, 1]), int(settings.c[label % 32, 2])), 4) elif self.__flag_mode == 3: self.infoBox.setText(u'当前为人体行为识别模式') humans = poseEstimator.inference(show) ori = np.copy(show) show, joints, bboxes, xcenter, sk = TfPoseEstimator.get_skeleton( show, humans, imgcopy=False) height = show.shape[0] width = show.shape[1] if bboxes: result = np.array(bboxes) det = result[:, 0:5] det[:, 0] = det[:, 0] * width det[:, 1] = det[:, 1] * height det[:, 2] = det[:, 2] * width det[:, 3] = det[:, 3] * height trackers = self.tracker.update(det) #有几个框就生成几个追踪器 self.current = [i[-1] for i in trackers] if len(self.previous) > 0: for item in self.previous: if item not in self.current and item in self.data: del self.data[item] if item not in self.current and item in self.memory: del self.memory[item] self.previous = self.current for d in trackers: xmin = int(d[0]) ymin = int(d[1]) xmax = int(d[2]) ymax = int(d[3]) label = int(d[4]) try: j = np.argmin( np.array([ abs(i - (xmax + xmin) / 2.) for i in xcenter ])) except: j = 0 if joint_filter(joints[j]): pred = 0 joints[j] = joint_completion( joint_completion(joints[j])) if len(joints[j]) == 18: if label not in self.data: self.data[label] = [ joints[j] ] #self.data[label]:[{0:(xj1,yj1),1:(xj2,yj2),...,n:(xjn,yjn)},{},{}] self.memory[label] = 0 else: self.data[label].append(joints[j]) #print('len(self.data[label]):{0}'.format(len(self.data[label]))) if len( self.data[label] ) == settings.L: #self.data[label]:[16,{18,key:(x,y)}] for i in range(settings.L): keys = self.data[label][i].keys() keys = sorted(keys) for key in keys: self.one_frame.append( np.array(self.data[label][i][key])) if label not in self.pred_data: self.pred_data[label] = [ np.array(self.one_frame) ] else: self.pred_data[label].append( np.array(self.one_frame) ) #[[[x0 y0],..[x17 y17]],..[]] self.one_frame = [] self.pred_data[label] = np.array( self.pred_data[label]) pred = sk_cnn_actionPredicter.predict( self.pred_data[label]) #print('预测动作下标:{0}'.format(pred)) self.pred_data[label] = [] self.data[label].pop(0) location = self.data[label][-1][1] if location[0] <= 30: location = (51, location[1]) if location[1] <= 10: location = (location[0], 31) #print('预测动作类别:{0}'.format(settings.move_status[pred])) cv2.putText(show, settings.move_status[pred], (location[0] - 30, location[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) cv2.rectangle(show, (xmin, ymin), (xmax, ymax), (int(settings.c[label % 32, 0]), int(settings.c[label % 32, 1]), int(settings.c[label % 32, 2])), 4) end = time.time() self.fps = 1. / (end - start) cv2.putText(show, 'FPS: %.2f' % self.fps, (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888) self.label_show_camera.setPixmap( QtGui.QPixmap.fromImage(showImage))
def show_camera(self, frame): start = time.time() # ret, frame = self.cap.read() show = cv2.resize(frame, (settings.winWidth, settings.winHeight)) show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB) if ret: if self.__flag_mode == 1: humans = poseEstimator.inference(show) show = TfPoseEstimator.draw_humans(show, humans, imgcopy=False) elif self.__flag_mode == 2: humans = poseEstimator.inference(show) show, joints, bboxes, xcenter, sk = TfPoseEstimator.get_skeleton( show, humans, imgcopy=False) height = show.shape[0] width = show.shape[1] if bboxes: result = np.array(bboxes) det = result[:, 0:5] det[:, 0] = det[:, 0] * width det[:, 1] = det[:, 1] * height det[:, 2] = det[:, 2] * width det[:, 3] = det[:, 3] * height trackers = self.tracker.update(det) for d in trackers: xmin = int(d[0]) ymin = int(d[1]) xmax = int(d[2]) ymax = int(d[3]) label = int(d[4]) cv2.rectangle(show, (xmin, ymin), (xmax, ymax), (int(settings.c[label % 32, 0]), int(settings.c[label % 32, 1]), int(settings.c[label % 32, 2])), 4) # This is the part to get the kt points elif self.__flag_mode == 3: humans = poseEstimator.inference(show) ori = np.copy(show) show, joints, bboxes, xcenter, sk = TfPoseEstimator.get_skeleton( show, humans, imgcopy=False) height = show.shape[0] width = show.shape[1] if bboxes: result = np.array(bboxes) det = result[:, 0:5] det[:, 0] = det[:, 0] * width det[:, 1] = det[:, 1] * height det[:, 2] = det[:, 2] * width det[:, 3] = det[:, 3] * height trackers = self.tracker.update(det) self.current = [i[-1] for i in trackers] if len(self.previous) > 0: for item in self.previous: if item not in self.current and item in self.data: del self.data[item] if item not in self.current and item in self.memory: del self.memory[item] self.previous = self.current for d in trackers: xmin = int(d[0]) ymin = int(d[1]) xmax = int(d[2]) ymax = int(d[3]) label = int(d[4]) try: j = np.argmin( np.array([ abs(i - (xmax + xmin) / 2.) for i in xcenter ])) except: j = 0 if joint_filter(joints[j]): joints[j] = joint_completion( joint_completion(joints[j])) if label not in self.data: self.data[label] = [joints[j]] self.memory[label] = 0 else: self.data[label].append(joints[j]) if len(self.data[label]) == settings.L: pred = actionPredictor().move_status( self.data[label]) if pred == 0: pred = self.memory[label] else: self.memory[label] = pred self.data[label].pop(0) location = self.data[label][-1][1] if location[0] <= 30: location = (51, location[1]) if location[1] <= 10: location = (location[0], 31) cv2.putText( show, settings.move_status[pred], (location[0] - 30, location[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) cv2.rectangle(show, (xmin, ymin), (xmax, ymax), (int(settings.c[label % 32, 0]), int(settings.c[label % 32, 1]), int(settings.c[label % 32, 2])), 4) cv2.imshow('window', cv2.resize(show, (432, 368))) self.out.write(show) # Write out frame to video end = time.time()
def _perform_estimation_(self): self.humans = self.estimator.inference(self.image) self.image = TfPoseEstimator.draw_humans(self.image, self.humans, imgcopy=False)
def show_video(self): self.video_event = True start = time.time() cap = cv2.VideoCapture('22_360p.mp4') while (cap.isOpened()): ret, frame = cap.read() show = cv2.resize(frame, (settings.winWidth, settings.winHeight)) show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB) if ret: if self.__flag_mode == 1: self.infoBox.setText(u'현재 인간 포즈 추정 모드') humans = poseEstimator.inference(show) show = TfPoseEstimator.draw_humans(show, humans, imgcopy=False) elif self.__flag_mode == 2: self.infoBox.setText(u'현재 다중 인간 포즈 추정 모드') humans = poseEstimator.inference(show) show, joints, bboxes, xcenter, sk = TfPoseEstimator.get_skeleton( show, humans, imgcopy=False) height = show.shape[0] width = show.shape[1] if bboxes: result = np.array(bboxes) det = result[:, 0:5] det[:, 0] = det[:, 0] * width det[:, 1] = det[:, 1] * height det[:, 2] = det[:, 2] * width det[:, 3] = det[:, 3] * height trackers = self.tracker.update(det) for d in trackers: xmin = int(d[0]) ymin = int(d[1]) xmax = int(d[2]) ymax = int(d[3]) label = int(d[4]) cv2.rectangle(show, (xmin, ymin), (xmax, ymax), (int(settings.c[label % 32, 0]), int(settings.c[label % 32, 1]), int(settings.c[label % 32, 2])), 4) elif self.__flag_mode == 3: self.infoBox.setText(u'현재 인간 행동 인식 모드') humans = poseEstimator.inference(show) ori = np.copy(show) show, joints, bboxes, xcenter, sk = TfPoseEstimator.get_skeleton( show, humans, imgcopy=False) height = show.shape[0] width = show.shape[1] if bboxes: result = np.array(bboxes) det = result[:, 0:5] det[:, 0] = det[:, 0] * width det[:, 1] = det[:, 1] * height det[:, 2] = det[:, 2] * width det[:, 3] = det[:, 3] * height trackers = self.tracker.update(det) self.current = [i[-1] for i in trackers] if len(self.previous) > 0: for item in self.previous: if item not in self.current and item in self.data: del self.data[item] if item not in self.current and item in self.memory: del self.memory[item] self.previous = self.current for d in trackers: xmin = int(d[0]) ymin = int(d[1]) xmax = int(d[2]) ymax = int(d[3]) label = int(d[4]) try: j = np.argmin( np.array([ abs(i - (xmax + xmin) / 2.) for i in xcenter ])) except: j = 0 if joint_filter(joints[j]): joints[j] = joint_completion( joint_completion(joints[j])) if label not in self.data: self.data[label] = [joints[j]] self.memory[label] = 0 else: self.data[label].append(joints[j]) if len(self.data[label]) == settings.L: pred = actionPredictor().move_status( self.data[label]) if pred == 0: pred = self.memory[label] else: self.memory[label] = pred self.data[label].pop(0) location = self.data[label][-1][1] if location[0] <= 30: location = (51, location[1]) if location[1] <= 10: location = (location[0], 31) cv2.putText( show, settings.move_status[pred], (location[0] - 30, location[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2) cv2.rectangle(show, (xmin, ymin), (xmax, ymax), (int(settings.c[label % 32, 0]), int(settings.c[label % 32, 1]), int(settings.c[label % 32, 2])), 4) end = time.time() self.fps = 1. / (end - start) cv2.putText(show, 'FPS: %.2f' % self.fps, (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888) self.label_show_camera.setPixmap( QtGui.QPixmap.fromImage(showImage)) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release()
def show_camera(self): start = time.time() ret, frame = self.cap.read() show = cv2.resize(frame, (settings.winWidth, settings.winHeight)) show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB) if ret: if self.__flag_mode == 1: self.infoBox.setText(u'当前为人体姿态估计模式') humans = poseEstimator.inference(show) show = TfPoseEstimator.draw_humans(show, humans, imgcopy=False) elif self.__flag_mode == 2: self.infoBox.setText(u'当前为多人跟踪模式') humans = poseEstimator.inference(show) show, joints, bboxes, xcenter, sk = TfPoseEstimator.get_skeleton(show, humans, imgcopy=False) height = show.shape[0] width = show.shape[1] if bboxes: result = np.array(bboxes) det = result[:, 0:5] det[:, 0] = det[:, 0] * width det[:, 1] = det[:, 1] * height det[:, 2] = det[:, 2] * width det[:, 3] = det[:, 3] * height trackers = self.tracker.update(det) for d in trackers: xmin = int(d[0]) ymin = int(d[1]) xmax = int(d[2]) ymax = int(d[3]) label = int(d[4]) cv2.rectangle(show, (xmin, ymin), (xmax, ymax), (int(settings.c[label % 32, 0]), int(settings.c[label % 32, 1]), int(settings.c[label % 32, 2])), 4) elif self.__flag_mode == 3: self.infoBox.setText(u'当前为人体行为识别模式') humans = poseEstimator.inference(show) show = TfPoseEstimator.draw_humans(show, humans, imgcopy=False) ori = np.copy(show) show, joints, bboxes, xcenter, sk= TfPoseEstimator.get_skeleton(show, humans, imgcopy=False) height = show.shape[0] width = show.shape[1] if bboxes: result = np.array(bboxes) det = result[:, 0:5] det[:, 0] = det[:, 0] * width det[:, 1] = det[:, 1] * height det[:, 2] = det[:, 2] * width det[:, 3] = det[:, 3] * height trackers = self.tracker.update(det) self.current = [i[-1] for i in trackers] if len(self.previous) > 0: for item in self.previous: if item not in self.current and item in self.data: del self.data[item] if item not in self.current and item in self.memory: del self.memory[item] self.previous = self.current for d in trackers: xmin = int(d[0]) ymin = int(d[1]) xmax = int(d[2]) ymax = int(d[3]) label = int(d[4]) try: j = np.argmin(np.array([abs(i - (xmax + xmin) / 2.) for i in xcenter])) except: j = 0 alert = False if joint_filter(joints[j]): joints[j] = joint_completion(joint_completion(joints[j])) if label not in self.data: self.data[label] = [joints[j]] self.memory[label] = 0 else: self.data[label].append(joints[j]) if len(self.data[label]) == settings.L: pred = actionPredictor().move_status(self.data[label]) if pred == 0: pred = self.memory[label] else: self.memory[label] = pred self.data[label].pop(0) location = self.data[label][-1][1] if location[0] <= 30: location = (51, location[1]) if location[1] <= 10: location = (location[0], 31) ## Use simsum.ttc to write Chinese. b, g, r, a = 0, 255, 0, 0 img_pil = Image.fromarray(show) draw = ImageDraw.Draw(img_pil) # print('pred', pred) if pred == 8: draw.text((location[0] - 30, location[1] - 10), settings.move_status[pred], font=self.font, fill=(255, 0, 0, a)) alert = True else: draw.text((location[0] - 30, location[1] - 10), settings.move_status[pred], font=self.font, fill=(b, g, r, a)) show = np.array(img_pil) # cv2.putText(show, settings.move_status[pred], (location[0] - 30, location[1] - 10), # cv2.FONT_HERSHEY_SIMPLEX, 0.8, # (0, 255, 0), 2) if alert: colors = (255, 0, 0) else: colors = (int(settings.c[label % 32, 0]), int(settings.c[label % 32, 1]), int(settings.c[label % 32, 2])) cv2.rectangle(show, (xmin, ymin), (xmax, ymax), colors, 4) if alert: cv2.imwrite('files/alerts/fall_' + str(int(time.time())) + '.png', show) end = time.time() self.fps = 1. / (end - start) cv2.putText(show, 'FPS: %.2f' % self.fps, (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888) self.label_show_camera.setPixmap(QtGui.QPixmap.fromImage(showImage))