def handle_pose_measuring(self) -> None: self.__display_figure() elapsed = perf_counter() - self.t_start self.poses_ui[self.current_pose_i].alpha_composite( self.frame, 60, self.height - 220, elapsed) pose = self.poses[self.current_pose_i] if not self.keypoints_detected: self.state = State.PoseReady return person = self.keypoints[0] self.angle = pose.angle_difference(person) self.confidence = pose.average_confidence(person) self.score = pose.compute_matching_score(person) put_text( self.frame, f'angle: {self.angle:.3f} / score: {self.score:.3f} / conf: {self.confidence:.3f}', (self.border_margin, self.height - self.border_margin), (255, 255, 255)) # 만약 측정 도중 자세가 틀어지면 다시 PoseReady로 self.fail_counter.append(self.score < pose.threshold) if self.check_failed(): self.state = State.PoseReady if elapsed >= pose.duration: try: self.current_pose_i = next(self.pose_index_iter) self.state = State.PoseReady except StopIteration: self.state = State.Finish
def classify_frame(capture, model, label): """Use the model to predict the class label. """ _, frame = capture.read() # Capture frame-by-frame _, ind, prob = model.predict(Image(utils.cv2torch(frame))) utils.put_text(frame, f"{label[ind]} ({prob[ind]:.2f})") return utils.cv2matplotlib(frame)
def run(self): datum = self._datum while self.running: ret, frame = self.cap.read() if not ret: break datum.cvInputData = frame self._op_wrapper.emplaceAndPop([datum]) self.keypoints = datum.poseKeypoints self.frame = datum.cvOutputData self.handlers[self.state]() put_text(self.frame, self.state.name, (self.border_margin, self.border_margin), (0, 255, 0)) cv.imshow(self.window_name, self.frame) key = cv.waitKey(1) & 0xff self.handle_input(key) self.recorder.write_data(self.frame, self.keypoints, self.state.name, self.poses[self.current_pose_i].name, self.angle, self.confidence, self.score) self.close()
def add_to_debugbar(self, base, inset, msg, position='right'): # ysize = 400 # Size of Debug Bar in px text_ysize = 42 # Height of Text with Padding right_padding = 30 # Distance from Right Edge hz_centering_msg = 85 additional_text_offset = 5 # Resize inset = cv2.resize(inset, None, fx=0.4, fy=0.4) # Position if position == 'left': (x, y) = (right_padding * 2), (text_ysize + right_padding) else: (x, y) = (base.shape[1] - inset.shape[1] - right_padding * 2), (text_ysize + right_padding) # Embed if inset.ndim == 2: # Adaptive Rescale and Convert to Color inset = cv2.cvtColor((inset / inset.max()).astype('float32'), cv2.COLOR_GRAY2RGB) # inset = cv2.cvtColor(((inset)*254./(inset.max())).astype('uint8'), cv2.COLOR_GRAY2RGB) base[y:y + inset.shape[0], x:x + inset.shape[1], :] = inset # Title Text (xpos, ypos) = (x + hz_centering_msg), (text_ysize + additional_text_offset) put_text(base, msg, xpos, ypos) return base
def display_counters(frame, counter_logs): h, w, _ = frame.shape align_x = int(w * 0.63) align_y = int(h * 0.3) for i, (k, v) in enumerate(counter_logs.items()): text = "{}: {}".format(k, v).ljust(11).lower() put_text(frame, text, (align_x, 15 * (i + 1) + align_y))
def moveing_target_detect(video_file,target_name,meta_path,model_path): sess = tf.Session() cv2.namedWindow('frame') cv2.namedWindow('foreground') cv2.moveWindow('frame', 10, 200) cv2.moveWindow('foreground', 800, 200) input_x, pre_softmax_ = utils.load_model(sess,meta_path=meta_path,model_path=model_path)# 加载训练好的模型 cap = cv2.VideoCapture(video_file) GMM = cv2.createBackgroundSubtractorMOG2(history=5000, varThreshold=10)#混合高斯背景建模 framenum = 0 while(1): roi_list = [] index_list = [] cache_coordinate = [] ret,frame = cap.read() frame=cv2.resize(frame,(800,576)) GMM_img = GMM.apply(frame,learningRate=-1)#前景提取 ret1 ,foreground = cv2.threshold(GMM_img,200,255,cv2.THRESH_BINARY)#二值化 foreground = utils.Morphological_processing(foreground) #形态学处理 _, contours_filled, hi = cv2.findContours(foreground, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #寻找运动目标对应的原图区域 for index,contour in enumerate(contours_filled): x,y,w,h=cv2.boundingRect(contour) if w*h>=50: x,y,w,h = utils.get_bigger_contour(x, y, w, h, width=50) cache_coordinate.append((x,y,w,h)) roi = frame[y:y + h, x:x + w] roi = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB) roi = cv2.resize(roi, (128, 128)) roi = np.array(roi) * (1. / 255) roi_list.append(roi) index_list.append(index) #对运动目标预测分类,并框出感兴趣运动目标 if len(index_list)!=0: roi_imgs=np.array(roi_list) pre_softmax=sess.run(pre_softmax_,feed_dict={input_x:roi_imgs}) pre_argmax=np.argmax(pre_softmax,1) pre_argmax_list,pre_softmax_list=list(pre_argmax),list(pre_softmax) for index,pre in enumerate(pre_argmax_list): if pre==dict[target_name] and pre_softmax_list[index][dict[target_name]] >= 0.8: score = pre_softmax_list[index][dict[target_name]] x,y,w,h = cache_coordinate[index] cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2) utils.put_text(frame,target_name,score,x,y) framenum = framenum + 1 cv2.imshow('frame',frame) cv2.imshow('foreground',foreground) cv2.waitKey(10) cap.release() cv2.destroyAllWindows() sess.close()
def display_decisions(frame, head_action, eye_action): h, w, _ = frame.shape print("=================================") align_x, align_y = int(w * 0.05), int(h * 0.8) print(head_action) print(eye_action) put_text(frame, str(head_action)[11:], (align_x, align_y + 30), scale=1.5, thickness=2) put_text(frame, str(eye_action)[10:], (align_x, align_y + 3 * 30), scale=1.5, thickness=2)
def visualize_params(img, params_list): k = max(img.shape) / 1024 t = round(k) scale = k * 1.2 for params in params_list: # contours e_color, cnt_color = (0, 0, 255), (0, 255, 0) if params['on_edge']: e_color, cnt_color = (0, 0, 130), (0, 130, 0) cv.ellipse(img, params['ellipse'], e_color, t, cv.LINE_AA) cv.drawContours(img, [params['cnt']], -1, cnt_color, t, cv.LINE_AA) # text y1, x1, y2, x2 = params['box'] text = '{id} {score:.2f}'.format(**params) utils.put_text(img, text, x1, y1)
def handle_pose_ready(self) -> None: self.__display_figure() self.poses_ui[self.current_pose_i].alpha_composite( self.frame, 60, self.height - 220, 0) pose = self.poses[self.current_pose_i] self.angle = np.pi self.confidence = 0 self.score = 0 if not self.keypoints_detected: return person = self.keypoints[0] self.angle = pose.angle_difference(person) self.confidence = pose.average_confidence(person) self.score = pose.compute_matching_score(person) put_text( self.frame, f'angle: {self.angle:.3f} / score: {self.score:.3f} / conf: {self.confidence:.3f}', (self.border_margin, self.height - self.border_margin), (255, 255, 255)) if self.score >= pose.threshold: self.t_start = perf_counter() self.state = State.PoseMeasuring
import cv2 import os from time import sleep import utils as ut video = cv2.VideoCapture(0) while True: ret, img = video.read() faces, gray_img = ut.face_detection(img) for face in faces: (x, y, w, h) = face print("Face identificada!\nPosicao: ({}, {})\tTamanho: ({}px, {}px)\n". format(x, y, w, h)) ut.draw_rect(img, face) ut.put_text(img, "Pessoa", x, y) resized_img = cv2.resize(img, (1000, 700)) cv2.imshow('Video', resized_img) if cv2.waitKey(1) & 0xFF == ord('q'): break video.release() cv2.destroyAllWindows()
def display_results(img,det,gt_boxes,class_names): # from utils import getpallete # palette = getpallete(256) from dataset.cs_labels import labels lut = np.zeros((256,3)) for l in labels: if l.trainId<255 and l.trainId>=0: lut[l.trainId,:]=list(l.color) palette = lut det2seg = {0:6,1:7,2:11,3:12,4:13,5:14,6:15,7:16,8:17,9:18,} if DEBUG: print({"out_img":out_img.shape,"label_img":label_img.shape,"img":img.shape}) # lut_reshaped = np.array(palette).astype(np.uint8).reshape((256,3)) # lut_b = lut_reshaped[:,0] # lut_g = lut_reshaped[:,1] # lut_r = lut_reshaped[:,2] # print np.vstack((lut_r[:10],lut_g[:10],lut_b[:10])) # out_img = np.squeeze(self.executor.outputs[0].asnumpy().argmax(axis=1).astype(np.uint8)) # out_img_r = cv2.LUT(out_img,lut_r) # out_img_g = cv2.LUT(out_img,lut_g) # out_img_b = cv2.LUT(out_img,lut_b) # out_img = cv2.merge((out_img_r,out_img_g,out_img_b)) # label_img = data[label_name].astype(np.uint8) # label_img = np.swapaxes(label_img, 1, 2) # label_img = np.swapaxes(label_img, 0, 2).astype(np.uint8) # label_img_r = cv2.LUT(label_img,lut_r) # label_img_g = cv2.LUT(label_img,lut_g) # label_img_b = cv2.LUT(label_img,lut_b) # label_img = cv2.merge((label_img_r,label_img_g,label_img_b)) # img = np.squeeze(data[data_name]) img = (img + np.array([123.68, 116.779, 103.939]).reshape((3,1,1))).astype(np.uint8) img = np.swapaxes(img, 1, 2) img = np.swapaxes(img, 0, 2).astype(np.uint8) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # detection result det_img = img.copy() dets = det[np.where(det[:,0]>=0),:].reshape((-1,7)) if DEBUG: print(dets[:2,:]) # idx = nms(np.hstack((dets[:,2:6],dets[:,1:2])),.85) # dets = dets[idx,:] idx = np.argsort(dets[:,6],axis=0)[::-1] ## draw nearest first !! dets = dets[idx,:] h, w, ch = img.shape fontScale = .8*(h/float(320)) thickness = 2 if h>320 else 1 for idx in range(dets.shape[0]): # if dets[idx,1]<.15: # continue bbox = [int(round(dets[idx,2]*w)),int(round(dets[idx,3]*h)), \ int(round(dets[idx,4]*w)),int(round(dets[idx,5]*h))] color = palette[det2seg[int(dets[idx,0])],:] cv2.rectangle(det_img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color=(color[2],color[1],color[0]), thickness=thickness) clsname = class_names[int(dets[idx,0])] clsname_short = short_class_name[clsname] # text = "%s:%.0fm" % (clsname_short,dets[idx,6]*255.,) text = "%.0fm" % (dets[idx,6]*255.,) put_text(det_img, text, bbox, fontScale=fontScale) for box in gt_boxes.tolist(): bbox = [int(round(box[1]*w)),int(round(box[2]*h)), \ int(round(box[3]*w)),int(round(box[4]*h))] cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color=(0,0,128), thickness=thickness) clsname = class_names[int(box[0])] clsname_short = short_class_name[clsname] text = "%s:%.0fm" % (clsname_short,box[5]*255.,) put_text(img, text, bbox, fontScale=fontScale) if DEBUG: print("img.shape,label_img.shape,out_img.shape", \ img.shape,label_img.shape,out_img.shape) # if img.shape[0]!=out_img.shape[0]: # out_img = cv2.resize(out_img,(img.shape[1],img.shape[0]),interpolation=cv2.INTER_NEAREST) # label_img = cv2.resize(label_img,(img.shape[1],img.shape[0]),interpolation=cv2.INTER_NEAREST) if 1: # for training data with labels displayimg = np.hstack((img,det_img)) else: # for evaluation_only is ture displayimg = np.vstack((det_img,out_img)) seg_labels = get_seg_labels((30, displayimg.shape[1],3)) displayimg = np.vstack((displayimg,seg_labels)) if False: #displayimg.shape[0]>1000: hh, ww, ch = displayimg.shape displayimg_resized = cv2.resize(displayimg, (int(ww*.8),int(hh*.8))) else: displayimg_resized = displayimg cv2.imshow('out_img',displayimg_resized); # [exit(0) if (cv2.waitKey()&0xff)==27 else None] # cv2.imwrite('tmp/out_img_%03d.png'%(outimgiter,),displayimg); return displayimg
} while True: ret, img = video.read() faces, gray_img = ut.face_detection(img) for face in faces: (x, y, w, h) = face face_gray = gray_img[y:y+w, x:x+h] if model is None: label, confidence = ("Unknown", 1) else: label, confidence = model.predict(face_gray) name = names.get(label, str(label)) print("label: {}, confidence: {}". format(name, confidence)) ut.draw_rect(img, face) if confidence > 0.8: ut.put_text(img, name, x, y) resized_img = cv2.resize(img, (1000, 700)) cv2.imshow('Video', resized_img) if cv2.waitKey(1) & 0xFF == ord('q'): break video.release() cv2.destroyAllWindows()
window = frame[ref:2 * ref, ref:2 * ref, :] prediction = utils.pred_window(window) frame = utils.put_letter(frame, prediction) # Input Logic key = cv2.waitKey(1) if key == ord('q'): break elif key == ord('d'): detecting = not detecting elif key == ord('c'): if len(code) < 3: code += prediction text = code + '-' * (3 - len(code)) if len(code) == 3: text = '---' elif correct_count != 3 and prediction == code[correct_count]: correct_count += 1 text = '*' * correct_count + '-' * (3 - correct_count) if correct_count == 3: text = 'AUTHORIZED' # show image frame = utils.put_text(frame, text) cv2.imshow("Capturing", frame) vc.release() cv2.destroyAllWindows()
def display_results(out_img, label_img, img, det, gt_boxes, class_names): # from utils import getpallete # palette = getpallete(256) from dataset.cs_labels import labels lut = np.zeros((256, 3)) for l in labels: if l.trainId < 255 and l.trainId >= 0: lut[l.trainId, :] = list(l.color) palette = lut from detect.nms import nms import cv2 if DEBUG: print({ "out_img": out_img.shape, "label_img": label_img.shape, "img": img.shape }) lut_b = np.array(palette).astype(np.uint8).reshape((256, 3))[:, 0] lut_g = np.array(palette).astype(np.uint8).reshape((256, 3))[:, 1] lut_r = np.array(palette).astype(np.uint8).reshape((256, 3))[:, 2] # print np.vstack((lut_r[:10],lut_g[:10],lut_b[:10])) # out_img = np.squeeze(self.executor.outputs[0].asnumpy().argmax(axis=1).astype(np.uint8)) out_img_r = cv2.LUT(out_img, lut_r) out_img_g = cv2.LUT(out_img, lut_g) out_img_b = cv2.LUT(out_img, lut_b) out_img = cv2.merge((out_img_r, out_img_g, out_img_b)) # label_img = data[label_name].astype(np.uint8) label_img = np.swapaxes(label_img, 1, 2) label_img = np.swapaxes(label_img, 0, 2).astype(np.uint8) label_img_r = cv2.LUT(label_img, lut_r) label_img_g = cv2.LUT(label_img, lut_g) label_img_b = cv2.LUT(label_img, lut_b) label_img = cv2.merge((label_img_r, label_img_g, label_img_b)) # img = np.squeeze(data[data_name]) img = (img + np.array([123.68, 116.779, 103.939]).reshape( (3, 1, 1))).astype(np.uint8) img = np.swapaxes(img, 1, 2) img = np.swapaxes(img, 0, 2).astype(np.uint8) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # detection result det_img = img.copy() dets = det[np.where(det[:, 0] >= 0), :].reshape((-1, 7)) if DEBUG: print(dets[:2, :]) # idx = nms(np.hstack((dets[:,2:6],dets[:,1:2])),.9) # dets = dets[idx,:] h, w, ch = img.shape for idx in range(dets.shape[0]): # if dets[idx,1]<.15: # continue bbox = [int(round(dets[idx,2]*w)),int(round(dets[idx,3]*h)), \ int(round(dets[idx,4]*w)),int(round(dets[idx,5]*h))] cv2.rectangle(det_img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color=(0, 0, 128), thickness=1) text = "%s:%.0fm" % ( class_names[int(dets[idx, 0])], dets[idx, 6] * 255., ) put_text(det_img, text, bbox, fontScale=.8) for box in gt_boxes.tolist(): bbox = [int(round(box[1]*w)),int(round(box[2]*h)), \ int(round(box[3]*w)),int(round(box[4]*h))] if (bbox[2] - bbox[0]) * (bbox[3] - bbox[1]) < 100: continue cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color=(0, 0, 128), thickness=1) text = "%s:%.0fm" % ( class_names[int(box[0])], box[5] * 255., ) put_text(img, text, bbox, fontScale=.8) if DEBUG: print({ "img.shape": img.shape, "label_img.shape": label_img.shape, "out_img.shape": out_img.shape }) if img.shape[0] != out_img.shape[0]: out_img = cv2.resize(out_img, (img.shape[1], img.shape[0]), interpolation=cv2.INTER_NEAREST) label_img = cv2.resize(label_img, (img.shape[1], img.shape[0]), interpolation=cv2.INTER_NEAREST) displayimg = np.vstack((np.hstack( (img, label_img)), np.hstack((det_img, out_img)))) displayimg_resized = cv2.resize(displayimg, (int(w * 2 * .8), int(h * 2 * .8))) cv2.imshow('out_img', displayimg_resized) # cv2.imwrite('tmp/out_img_%03d.png'%(outimgiter,),displayimg); return displayimg
def sliding_window_search(self, image): try: start = time.time() # Uncomment the following line if you extracted training # data from .png images (scaled 0 to 1 by mpimg) and the # image you are searching is a .jpg (scaled 0 to 255) image = image.astype(np.float32) / 255 windows = slide_window(image, x_start_stop=[None, None], y_start_stop=self.y_start_stop, xy_window=self.xy_window, xy_overlap=self.xy_overlap) hot_windows = search_windows(image, windows, self.svc, self.X_scaler, color_space=self.color_space, spatial_size=self.spatial_size, hist_bins=self.hist_bins, orient=self.orient, pix_per_cell=self.pix_per_cell, cell_per_block=self.cell_per_block, hog_channel=self.hog_channel, spatial_feat=self.spatial_feat, hist_feat=self.hist_feat, hog_feat=self.hog_feat) end = time.time() self.windows = windows self.hot_windows = hot_windows draw_image = np.copy(image) msg = '%04d | Memory: %dFr | HeatTh: RollSum %d * CurFr %d | Accuracy: %0.1f%%' % ( self.count, MEMORY_SIZE, ROLLING_SUM_HEAT_THRESHOLD, CURRENT_FRAME_HEAT_THRESHOLD, ACCURACY / 100) self.update_overlay(draw_image) draw_image = weighted_img(draw_image, self.overlay) put_text(draw_image, msg) heat_thresholded_image, thresholded_heatmap, labels = self.heat_and_threshold( draw_image, self.hot_windows, rolling_threshold=ROLLING_SUM_HEAT_THRESHOLD, current_threshold=CURRENT_FRAME_HEAT_THRESHOLD) self.save = heat_thresholded_image except Exception as e: mpimg.imsave('hard/%d.jpg' % self.count, image) debug('Error(%s): Issue at Frame %d' % (str(e), self.count)) if DEBUG: import ipdb ipdb.set_trace() if self.save: heat_thresholded_image = self.save finally: self.count += 1 if VIDEO_MODE: # Scale Back to Format acceptable by moviepy heat_thresholded_image = heat_thresholded_image.astype( np.float32) * 255 else: debug('%0.1f seconds/frame. #%d/%d hot-windows/windows/frame' % (end - start, len(hot_windows), len(windows))) title1 = 'Car Positions (#Detections: %d)' % (labels[1]) title2 = 'Thresholded Heat Map (Max: %d)' % int( np.max(thresholded_heatmap)) imcompare(heat_thresholded_image, thresholded_heatmap, title1, title2, cmap2='hot') return heat_thresholded_image
h = bbox[3] - bbox[1] cv2.rectangle(display_img, (x, y), (x + w, y + h), (0, 255, 0), 2) if args.mode == "emo": points = handler.get(frame, bbox, get_all=True) display_img = draw_keypoints(display_img, points) points = points[0] preproc_img = preprocess(args, points, frame, [x, y, w, h]) emo = clf.predict(np.expand_dims(preproc_img, 0)) display_img = put_text(display_img, LABELS_DICT_EMO[np.argmax(emo[0])], (x, y - 20)) elif args.mode == "reco": f2 = model.get_feature(img) name = compare_emdbs(embds_dict, f2) display_img = put_text(display_img, name, (x, y - 40)) # print(sim, ": ", sim >= 0.5 and sim < 1.01) print("fps: ", 1 / (time.time() - time_1)) cv2.imshow("frame", display_img) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release()