def main(_argv): if FLAGS.tiny: STRIDES = np.array(cfg.YOLO.STRIDES_TINY) ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_TINY, FLAGS.tiny) else: STRIDES = np.array(cfg.YOLO.STRIDES) if FLAGS.model == 'yolov4': ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS, FLAGS.tiny) else: ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_V3, FLAGS.tiny) NUM_CLASS = len(utils.read_class_names(cfg.YOLO.CLASSES)) XYSCALE = cfg.YOLO.XYSCALE input_size = FLAGS.size video_path = FLAGS.video print("Video from: ", video_path ) vid = cv2.VideoCapture(video_path) height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) fps = int(vid.get(cv2.CAP_PROP_FPS)) fourcc = cv2.VideoWriter_fourcc(*'XVID') output_movie = cv2.VideoWriter('output' + str(round(time.time()))+ '.avi', fourcc, fps, (width, height)) if FLAGS.framework == 'tf': input_layer = tf.keras.layers.Input([input_size, input_size, 3]) if FLAGS.tiny: feature_maps = YOLOv3_tiny(input_layer, NUM_CLASS) bbox_tensors = [] for i, fm in enumerate(feature_maps): bbox_tensor = decode(fm, NUM_CLASS, i) bbox_tensors.append(bbox_tensor) model = tf.keras.Model(input_layer, bbox_tensors) utils.load_weights_tiny(model, FLAGS.weights) else: if FLAGS.model == 'yolov3': feature_maps = YOLOv3(input_layer, NUM_CLASS) bbox_tensors = [] for i, fm in enumerate(feature_maps): bbox_tensor = decode(fm, NUM_CLASS, i) bbox_tensors.append(bbox_tensor) model = tf.keras.Model(input_layer, bbox_tensors) utils.load_weights_v3(model, FLAGS.weights) elif FLAGS.model == 'yolov4': feature_maps = YOLOv4(input_layer, NUM_CLASS) bbox_tensors = [] for i, fm in enumerate(feature_maps): bbox_tensor = decode(fm, NUM_CLASS, i) bbox_tensors.append(bbox_tensor) model = tf.keras.Model(input_layer, bbox_tensors) if FLAGS.weights.split(".")[len(FLAGS.weights.split(".")) - 1] == "weights": utils.load_weights(model, FLAGS.weights) else: model.load_weights(FLAGS.weights).expect_partial() model.summary() else: # Load TFLite model and allocate tensors. interpreter = tf.lite.Interpreter(model_path=FLAGS.weights) interpreter.allocate_tensors() # Get input and output tensors. input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() print(input_details) print(output_details) total_passed_vehicle = 0 speed = "waiting..." direction = "waiting..." size = "waiting..." color = "waiting..." counting_mode = "..." width_heigh_taken = True while True: return_value, frame = vid.read() if return_value: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = Image.fromarray(frame) else: raise ValueError("No image! Try with another video format") frame_size = frame.shape[:2] image_data = utils.image_preprocess(np.copy(frame), [input_size, input_size]) image_data = image_data[np.newaxis, ...].astype(np.float32) prev_time = time.time() if FLAGS.framework == 'tf': pred_bbox = model.predict(image_data) else: interpreter.set_tensor(input_details[0]['index'], image_data) interpreter.invoke() pred_bbox = [interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details))] if FLAGS.model == 'yolov4': pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES, XYSCALE) else: pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES) bboxes = utils.postprocess_boxes(pred_bbox, frame_size, input_size, 0.25) boxes = bboxes[:, 0:4] scores = bboxes[:, 4] classes = bboxes[:, 5] #bboxes = utils.nms(bboxes, 0.213, method='nms') roi = 450 category_index = utils.read_class_names(cfg.YOLO.CLASSES) counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_image_array_y_axis(vid.get(1), frame, 1, False, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, y_reference = roi, use_normalized_coordinates=True, line_thickness=4) if counter == 1: cv2.line(frame, (roi, 0), (roi, height), (0, 0xFF, 0), 5) else: cv2.line(frame, (roi, 0), (roi, height), (0, 0, 0xFF), 5) total_passed_vehicle = total_passed_vehicle + counter # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText( input_frame, 'Veiculos Detectados: ' + str(total_passed_vehicle), (10, 35), font, 0.8, (0, 0xFF, 0xFF), 2, cv2.FONT_HERSHEY_SIMPLEX, ) cv2.putText( input_frame, 'Linha de ROI', (545, roi-10), font, 0.6, (0, 0, 0xFF), 2, cv2.LINE_AA, ) # image = utils.draw_bbox(frame, bboxes) # curr_time = time.time() # exec_time = curr_time - prev_time # result = np.asarray(image) # info = "time: %.2f ms" %(1000*exec_time) # print(info) # cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE) # result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # cv2.imshow("result", result) # if cv2.waitKey(1) & 0xFF == ord('q'): break output_movie.write(frame) print ("writing frame") if cv2.waitKey(1) & 0xFF == ord('q'): break vid.release() output_movie.release() cv2.destroyAllWindows()
def detect(save_img=False): out, source, weights, view_img, save_txt, imgsz, save_csv = \ opt.output, opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size, opt.save_csv webcam = source == '0' or source.startswith('rtsp') or source.startswith( 'http') or source.endswith('.txt') # Initialize device = select_device(opt.device) # if os.path.exists(out): # shutil.rmtree(out) # delete output folder # os.makedirs(out) # make new output folder half = device.type != 'cpu' # half precision only supported on CUDA # Load model model = attempt_load(weights, map_location=device) # load FP32 model imgsz = check_img_size(imgsz, s=model.stride.max()) # check img_size if half: model.half() # to FP16 # Second-stage classifier classify = False if classify: modelc = load_classifier(name='resnet101', n=2) # initialize modelc.load_state_dict( torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights modelc.to(device).eval() # Set Dataloader vid_path, vid_writer = None, None if webcam: view_img = True cudnn.benchmark = True # set True to speed up constant image size inference dataset = LoadStreams(source, img_size=imgsz) else: save_img = True dataset = LoadImages(source, img_size=imgsz) # Get names and colors names = model.module.names if hasattr(model, 'module') else model.names colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] # csv output csv_output = [] # Run inference t0 = time.time() img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img _ = model(img.half() if half else img ) if device.type != 'cpu' else None # run once for path, img, im0s, vid_cap in dataset: img = torch.from_numpy(img).to(device) img = img.half() if half else img.float() # uint8 to fp16/32 img /= 255.0 # 0 - 255 to 0.0 - 1.0 if img.ndimension() == 3: img = img.unsqueeze(0) # Inference t1 = time_synchronized() pred = model(img, augment=opt.augment)[0] # Apply NMS pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms) t2 = time_synchronized() # Apply Classifier if classify: pred = apply_classifier(pred, modelc, img, im0s) # Process detections for i, det in enumerate(pred): # detections per image if webcam: # batch_size >= 1 p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() else: p, s, im0 = path, '', im0s save_path = str(Path(out) / Path(p).name) txt_path = str(Path(out) / Path(p).stem) + ( '_%g' % dataset.frame if dataset.mode == 'video' else '') s += '%gx%g ' % img.shape[2:] # print string gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh if det is not None and len(det): # Rescale boxes from img_size to im0 size det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() # Print results for c in det[:, -1].unique(): n = (det[:, -1] == c).sum() # detections per class s += '%g %ss, ' % (n, names[int(c)]) # add to string # Write results for *xyxy, conf, cls in det: if save_txt: # Write to file with open(txt_path + '.txt', 'a') as f: xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format if save_csv: # csv output append xyxyNormalized = (torch.tensor(xyxy) / gn).tolist() csv_output.append([ Path(p).name, int(cls), float(conf), *xyxyNormalized ]) if save_img or view_img: # Add bbox to image # label = '%s' % (names[int(cls)]) label = '%s %.2f' % (names[int(cls)], conf) # plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=2) plot_one_box(xyxy, im0, label=label, color=colors[int(cls)]) # Print time (inference + NMS) print('%sDone. (%.3fs)' % (s, t2 - t1)) # Stream results if view_img: cv2.imshow(p, im0) if cv2.waitKey(1) == ord('q'): # q to quit raise StopIteration # Save results (image with detections) if save_img: if dataset.mode == 'images': cv2.imwrite(save_path, im0) else: if vid_path != save_path: # new video vid_path = save_path if isinstance(vid_writer, cv2.VideoWriter): vid_writer.release( ) # release previous video writer fourcc = 'mp4v' # output video codec fps = vid_cap.get(cv2.CAP_PROP_FPS) w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) vid_writer = cv2.VideoWriter( save_path, cv2.VideoWriter_fourcc(*fourcc), fps, (w, h)) vid_writer.write(im0) # write csv output if save_csv: current_time = datetime.datetime.now( pytz.timezone('Asia/Seoul')).strftime('%Y-%m-%d-%H-%M') with open( f'/DL_data_big/AI_factory_dataset/tracking_recognition/ai_factory_tracking/dataset/yolo_output/output_{current_time}.csv', 'w', newline='') as f: f_writer = csv.writer(f) f_writer.writerow([ 'ImageID', 'LabelName', 'Conf', 'XMin', 'XMax', 'YMin', 'YMax' ]) f_writer.writerows(csv_output) print("csv saved") if save_txt or save_img: print('Results saved to %s' % Path(out)) if platform == 'darwin' and not opt.update: # MacOS os.system('open ' + save_path) print('Done. (%.3fs)' % (time.time() - t0))
#import library import numpy as np import cv2 import os from pathlib import Path import re import time #enter the file name print("Enter the File Name:") file_name = input() #start recording cap = cv2.VideoCapture(0) #defining the protocol fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('test.avi', fourcc, 60, (320, 240)) #fourcc = cv2.VideoWriter_fourcc(*'MP4V') #out = cv2.VideoWriter(str(file_name)+'.mp4', fourcc, 20.0, (640, 480)) #video will record till Q or ESC pressed while 1: ret, img = cap.read() out.write(img) cv2.imshow('img',img) k = cv2.waitKey(30) & 0xff if k == 27: break
def rigid_djikstra(mazetype,start,goal,diameter,clearance): write_to_video=True mazetype="trial" d=(diameter/2)+clearance robot_radius=diameter/2 print("Sol of dijkstra") maze=mazemaker.mazeMaker("trial") if maze[goal[0]][goal[1]]==2 or maze[start[0]][start[1]]==2: print("Cannot solve as start or goal is inside obstacle") return maze[start[0]][start[1]]=5 maze[goal[0]][goal[1]]=6 maze_size=[len(maze),len(maze[0])] maze_height=len(maze) maze_width=len(maze[0]) my_maze=np.zeros((maze_size)) # Define the codec and initialize the output file if write_to_video: fourcc = cv2.VideoWriter_fourcc(*'XVID') today = time.strftime("%m-%d__%H.%M.%S") videoname="Rigid"+str(mazetype)+str(today) fps_out = 500 video_out = cv2.VideoWriter(str(videoname)+".avi", fourcc, fps_out, (maze_width, maze_height)) # video_out=cv2.VideoWriter(str(videoname)+".avi",cv2.VideoWriter_fourcc('M','J','P','G'),100,(maze_height,maze_width)) print("Writing to Video, Please Wait") my_maze=maze my_maze[start[0]][start[1]]=5 my_maze[goal[0]][goal[1]]=6 print(maze_size) output=np.zeros((maze_height, maze_width,3),np.uint8) for y in range (len(maze)): for x in range(len(maze[0])): if my_maze[y][x]==2: output[y][x]=(0,0,255) # cv2.imshow("Output", output) distance_from_start=np.zeros((maze_size)) parent=np.zeros(([maze_size[0],maze_size[1],2])) distance_from_start=np.full((maze_size[0],maze_size[1]), np.inf) numExpanded=0 distance_from_start[start[0]][start[1]]=0 current=start expanded=[] expanded.append(start) if robotInspace(start[0],start[1],d,maze)==False or robotInspace(goal[0],goal[1],d,maze)==False : print("Could not solve") return while 1: my_maze[start[0]][start[1]]=5 my_maze[goal[0]][goal[1]]=6 min_dist=float('inf') for c in expanded: if min_dist>distance_from_start[c[0]][c[1]]: min_dist=distance_from_start[c[0]][c[1]] current=c #print(current) if current==goal or min_dist==float('inf'): break my_maze[current[0]][current[1]]=3 #node is visited output=addToVideo(output,current[0],current[1],3,video_out,[False,0]) expanded.remove(current) distance_from_start[current[0]][current[1]]=float('inf') #so that next node is explored if current[1]<maze_size[1]-1 and robotInspace(current[0],current[1]+1,d,maze)==True: right1=[current[0],current[1]+1] else: right1=current #right if current[1]>0 and robotInspace(current[0],current[1]-1,d,maze)==True: left1=[current[0],current[1]-1] else: #left left1=current if current[0]<maze_size[0]-1 and robotInspace(current[0]+1,current[1],d,maze)==True: down1=[current[0]+1,current[1]] else: #down down1=current if current[0]>0 and robotInspace(current[0]-1,current[1],d,maze)==True : up1=[current[0]-1,current[1]] else: #up up1=current if current[0]<maze_size[0]-1 and current[1]<maze_size[1]-1 and robotInspace(current[0]+1,current[1]+1,d,maze)==True : downright=[current[0]+1,current[1]+1] # down right else: downright=current if current[0]<maze_size[0]-1 and current[1]>0 and robotInspace(current[0]+1,current[1]-1,d,maze)==True: downleft=[current[0]+1,current[1]-1] # down left else: downleft=current if current[0]>0 and current[1]>0 and robotInspace(current[0]-1,current[1]-1,d,maze)==True : upleft=[current[0]-1,current[1]-1] # up left else: upleft=current if current[0]>0 and current[1]<maze_size[1]-1 and robotInspace(current[0]-1,current[1]+1,d,maze)==True : upright=[current[0]-1,current[1]+1] # up right else: upright=current adjacent=[] adjacent.append(up1) adjacent.append(down1) adjacent.append(left1) adjacent.append(right1) adjacent.append(upleft) adjacent.append(upright) adjacent.append(downleft) adjacent.append(downright) temp=0 for n in range(len(adjacent)): #Node is either unvisited/empty or looked at or goal that is should not be visited or should not be a obstacle if my_maze [int(adjacent[n][0])] [int(adjacent[n][1])] ==1 or my_maze[adjacent[n][0]][adjacent[n][1]]==4 or my_maze[adjacent[n][0]][adjacent[n][1]]==6 : if distance_from_start[int(adjacent[n][0])] [int(adjacent[n][1])] > min_dist+dist(current,adjacent[n]): distance_from_start[int(adjacent[n][0])] [int(adjacent[n][1])]=min_dist+dist(current,adjacent[n]) parent[int(adjacent[n][0])] [int(adjacent[n][1])]=current temp=n #so that we know the node entered this loop my_maze[int(adjacent[n][0])] [int(adjacent[n][1])]=4 #node is looked at output=addToVideo(output,int(adjacent[n][0]),int(adjacent[n][1]),4,video_out, [False,0]) expanded.append(adjacent[n]) numExpanded=numExpanded+1 current=adjacent[temp] #print(distance_from_start) #time.sleep(1) #print(parent) route=[] if distance_from_start[goal[0]][goal[1]]==float('inf'): route=[] else: route=[goal] #print(([route[len(route)-1][0]][route[len(route)-1][1]][0])) #print(int(parent[route[len(route)-1][0]][route[len(route)-1][1]][1])) a=route[len(route)-1][0] b=route[len(route)-1][1] #print(parent[a][b][0]) while parent[int(a)][int(b)][0]!=start[0] or parent[int(a)][int(b)][1]!=start[1]: a=route[len(route)-1][0] b=route[len(route)-1][1] c1=parent[int(a)][int(b)][0] c2=parent[int(a)][int(b)][1] route.append([c1,c2]) #print(route) for i in route: maze[int(i[0])][int(i[1])]=0 #Slow it down for j in range(100): output=addToVideo(output,int(i[0]),int(i[1]),0,video_out,[True,robot_radius]) print(i) maze[start[0]][start[1]]=5 maze[goal[0]][goal[1]]=6 # Pad some frames at the end of the video q=0 while q<1000: video_out.write(output) q+=1 cv2.imshow("Final",output) cv2.waitKey(0) print("Done!")
def plotHeatMap(inifile, animalbp1, mmSize, noIncrements, colorPalette, targetBehav, lastImageOnlyBol): config = ConfigParser() configFile = str(inifile) try: config.read(configFile) except MissingSectionHeaderError: print( 'ERROR: Not a valid project_config file. Please check the project_config.ini path.' ) projectPath = config.get('General settings', 'project_path') csv_dir_in = os.path.join(projectPath, 'csv', 'machine_results') vidLogFilePath = os.path.join(projectPath, 'logs', 'video_info.csv') videoLog = pd.read_csv(vidLogFilePath) trackedBodyParts = [str(animalbp1) + '_x', str(animalbp1) + '_y'] frames_dir_out = os.path.join(projectPath, 'frames', 'output', 'heatmap_behavior') if not os.path.exists(frames_dir_out): os.makedirs(frames_dir_out) vidInfPath = os.path.join(projectPath, 'logs', 'video_info.csv') vidinfDf = pd.read_csv(vidInfPath) colorList, loopCounter = [], 0 filesFound = glob.glob(csv_dir_in + '/*.csv') for currVid in filesFound: calcFlag = False loopCounter += 1 currVidBaseName = os.path.basename(currVid) currVidInfo = videoLog.loc[videoLog['Video'] == str( currVidBaseName.replace('.csv', ''))] fps, width, height = int(currVidInfo['fps']), int( currVidInfo['Resolution_width']), int( currVidInfo['Resolution_height']) pxPerMM = int(currVidInfo['pixels/mm']) binInput = int(mmSize * pxPerMM) outputfilename = os.path.join(frames_dir_out, currVidBaseName.replace('.csv', '.mp4')) binWidth, binHeight = (binInput, binInput) NbinsX, NbinsY, NbinsXCheck, NbinsYCheck = (int(width / binWidth), int(height / binHeight), width / binWidth, height / binHeight) targetCountArrayFrames = np.zeros((NbinsY, NbinsX)) im = np.zeros((height, width, 3)) im.fill(0) currDf = pd.read_csv(currVid) count_row = currDf.shape[0] vidInfo = vidinfDf.loc[vidinfDf['Video'] == str( currVidBaseName.replace('.csv', ''))] fps = int(vidInfo['fps']) im_scale_bar_width = int(width / 5) im_scale_bar = np.zeros((height, im_scale_bar_width, 3)) im_scale_bar_digits = np.zeros((height, im_scale_bar_width, 3)) im_scale_bar_digits_jumps = int(im_scale_bar_digits.shape[0] / 4) im_scale_bar_digits_jumps_List = np.arange( 0, im_scale_bar_digits.shape[0], im_scale_bar_digits_jumps).tolist() im_scale_bar_digits_jumps_List.append( int(im_scale_bar_digits.shape[0] - 10)) mySpaceScale, myRadius, myResolution, myFontScale = 60, 12, 1500, 1.5 maxResDimension = max(width, height) circleScale, fontScale, spacingScale = int( myRadius / (myResolution / maxResDimension)), float( myFontScale / (myResolution / maxResDimension)), int( mySpaceScale / (myResolution / maxResDimension)) if noIncrements != 'auto': noIncrements = int(noIncrements) cmap = cm.get_cmap(str(colorPalette), noIncrements + 1) for i in range(cmap.N): rgb = list((cmap(i)[:3])) rgb = [i * 255 for i in rgb] rgb.reverse() colorList.append(rgb) if noIncrements == 'auto': calcFlag = True print('auto') for index, row in currDf.iterrows(): if row[targetBehav] == 1: cordX, cordY = (row[trackedBodyParts[0]], row[trackedBodyParts[1]]) binX, binY = (int((cordX * NbinsX) / width), int((cordY * NbinsY) / height)) targetCountArrayFrames[ binY, binX] = targetCountArrayFrames[binY, binX] + 1 targetCountArray = ( (targetCountArrayFrames / fps) - 0.1) / 1 targetCountArray[targetCountArray < 0] = 0 noIncrements = math.ceil(np.max(targetCountArray)) cmap = cm.get_cmap(str(colorPalette), noIncrements + 1) for i in range(cmap.N): rgb = list((cmap(i)[:3])) rgb = [i * 255 for i in rgb] rgb.reverse() colorList.append(rgb) increments_digits_jump = noIncrements / 4 increments_digits_jump_list = np.arange( 0, noIncrements, increments_digits_jump).tolist() increments_digits_jump_list.append(noIncrements) if (calcFlag == False) and (lastImageOnlyBol == 1): for index, row in currDf.iterrows(): if row[targetBehav] == 1: cordX, cordY = (row[trackedBodyParts[0]], row[trackedBodyParts[1]]) binX, binY = (int((cordX * NbinsX) / width), int((cordY * NbinsY) / height)) targetCountArrayFrames[ binY, binX] = targetCountArrayFrames[binY, binX] + 1 targetCountArray = ( (targetCountArrayFrames / fps) - 0.1) / 1 targetCountArray[targetCountArray < 0] = 0 for i in range(cmap.N): topX = 0 try: topY = int((height / (noIncrements)) * i) except ZeroDivisionError: topY = 0 bottomX, bottomY = int(im_scale_bar_width), int( height / noIncrements) * cmap.N cv2.rectangle(im_scale_bar, (topX, topY - 4), (bottomX, bottomY), colorList[i], -1) if NbinsYCheck.is_integer(): pass else: frac = int((NbinsYCheck - int(NbinsYCheck)) * 100) newHeight = int(height - (frac * 0.5)) im_scale_bar = im_scale_bar[0:newHeight, 0:width] im_scale_bar_digits = im_scale_bar_digits[0:newHeight, 0:width] if NbinsXCheck.is_integer(): pass else: frac = int((NbinsXCheck - int(NbinsXCheck)) * 100) newWidth = int(width - (frac * 0.5)) im_scale_bar = im_scale_bar[0:height, 0:newWidth] im_scale_bar_digits = im_scale_bar_digits[0:height, 0:newWidth] for i in range(len(increments_digits_jump_list)): currY = im_scale_bar_digits_jumps_List[i] # if i == 0: # currY = currY + 15 # if i == len(increments_digits_jump_list)-1: # currY = currY - 15 if (i != 0) and (i != len(increments_digits_jump_list) - 1): currText = increments_digits_jump_list[i] cv2.putText(im_scale_bar_digits, str(currText), (10, currY), cv2.FONT_HERSHEY_COMPLEX, fontScale, (255, 255, 255), 1) im_scale_bar_text = np.concatenate((im_scale_bar, im_scale_bar_digits), axis=1) fourcc = cv2.VideoWriter_fourcc(*'mp4v') outputWidth = int(width + im_scale_bar_text.shape[0]) writer = cv2.VideoWriter(outputfilename, fourcc, fps, (outputWidth, height)) print('Calculating heatmap from file: ' + str(currVidBaseName) + '...') if lastImageOnlyBol == 1: print('last_only') for i in range(NbinsY): for j in range(NbinsX): if targetCountArray[i, j] >= noIncrements: targetCountArray[i, j] = noIncrements - 1 colorValue = colorList[int(targetCountArray[i, j])] topX = int(binWidth * j) topY = int(binHeight * i) bottomX = int(binWidth * (j + 1)) bottomY = int(binHeight * (i + 1)) currColor = tuple(colorValue) cv2.rectangle(im, (topX, topY), (bottomX, bottomY), currColor, -1) if NbinsYCheck.is_integer(): pass else: frac = int((NbinsYCheck - int(NbinsYCheck)) * 100) newHeight = int(height - (frac * 0.5)) im = im[0:newHeight, 0:width] if NbinsXCheck.is_integer(): pass else: frac = int((NbinsXCheck - int(NbinsXCheck)) * 100) newWidth = int(width - (frac * 0.5)) im = im[0:height, 0:newWidth] im = cv2.blur(im, (int(binWidth * 1.5), int(binHeight * 1.5))) imageConcat = np.concatenate((im, im_scale_bar_text), axis=1) imageConcat = cv2.resize(imageConcat, (outputWidth, height)) imageConcat = np.uint8(imageConcat) imageSaveName = os.path.join( frames_dir_out, currVidBaseName.replace('.csv', '.png')) cv2.imwrite(imageSaveName, imageConcat) print('Heatmap saved @: project_folder/frames/output/' + str(currVidBaseName.replace('.csv', '.png'))) if lastImageOnlyBol == 0: print('not_last_image') targetCountArrayFrames = np.zeros((NbinsY, NbinsX)) targetCountArray = targetCountArrayFrames.copy() for index, row in currDf.iterrows(): if row[targetBehav] == 1: cordX, cordY = (row[trackedBodyParts[0]], row[trackedBodyParts[1]]) binX, binY = (int((cordX * NbinsX) / width), int((cordY * NbinsY) / height)) targetCountArrayFrames[ binY, binX] = targetCountArrayFrames[binY, binX] + 1 targetCountArray = ( (targetCountArrayFrames / fps) - 0.1) / 1 targetCountArray[targetCountArray < 0] = 0 for i in range(NbinsY): for j in range(NbinsX): if targetCountArray[i, j] >= noIncrements: targetCountArray[i, j] = noIncrements - 1 colorValue = colorList[int(targetCountArray[i, j])] topX = int(binWidth * j) topY = int(binHeight * i) bottomX = int(binWidth * (j + 1)) bottomY = int(binHeight * (i + 1)) currColor = tuple(colorValue) cv2.rectangle(im, (topX, topY), (bottomX, bottomY), currColor, -1) if NbinsYCheck.is_integer(): pass else: frac = int((NbinsYCheck - int(NbinsYCheck)) * 100) newHeight = int(height - (frac * 0.5)) im = im[0:newHeight, 0:width] if NbinsXCheck.is_integer(): pass else: frac = int((NbinsXCheck - int(NbinsXCheck)) * 100) newWidth = int(width - (frac * 0.5)) im = im[0:height, 0:newWidth] im = cv2.blur(im, (int(binWidth * 1.5), int(binHeight * 1.5))) imageConcat = np.concatenate((im, im_scale_bar_text), axis=1) imageConcat = cv2.resize(imageConcat, (outputWidth, height)) imageConcat = np.uint8(imageConcat) writer.write(imageConcat) print('Image ' + str(index) + '/' + str(count_row) + '. Video ' + str(loopCounter) + '/' + str(len(filesFound))) print('All heatmaps generated in folder ' + 'project_folder/frames/output/heatmap_behavior') cv2.destroyAllWindows() writer.release()
#yield cv2.imencode('.jpg',frame)[1].tobytes() #options = { #'model': 'C:\DeepBlue\cfg\yolo.cfg', #'load': path, #'threshold': 0.5, #'gpu': 1.0 #} #tfnet = TFNet(options) colors = [tuple(255 * np.random.rand(3)) for _ in range(10)] oldTime = time.time() vid_cod = cv2.VideoWriter_fourcc(*'mp4v') resultK = cv2.VideoWriter('D:\DeepBlue\portal\demo1.mp4', vid_cod, 20.0, (640, 640)) print('oldTime', oldTime) while cap.isOpened(): _, frame = cap.read() frame = cv2.resize(frame, (640, 640)) #frame = cv2.resize(frame,(640,420),interpolation=cv2.INTER_AREA) print('Before frame') results = tfnet.return_predict(frame) print('got results')
print ('总帧数:',videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)) print ('帧率:',videoCapture.get(cv2.CAP_PROP_FPS)) fps = videoCapture.get(cv2.CAP_PROP_FPS) #获取原视频的帧率 # size = (int(600), int(1536))#自定义需要截取的画面的大小 size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))#获取原视频帧的大小 success = True frame_count = 0 all_frame = [] #videoWriter = cv2.VideoWriter('/Users/kj-xhzy/Documents/004_1_copy2.avi', cv2.VideoWriter_fourcc('M','J','P','G'), fps, size) while(success): success, frame = videoCapture.read() all_frame.append(frame) # params = [] # #params.append(cv.CV_IMWRITE_PXM_BINARY) # params.append(1) # cv2.imwrite("/Users/kj-xhzy/Documents/test1_frame/video" + "_%d.jpg" % frame_count, frame, params) # cv2.imwrite("/Users/kj-xhzy/Documents/test1jiance_frame/video" + "_%d.jpg" % frame_count, frame) frame_count = frame_count + 1 for i in range(0,frame_count,10): videoWriter = cv2.VideoWriter(path+file[:-4]+"_split_videos/"+file[:-4]+"_%d.mp4" % i, cv2.VideoWriter_fourcc('M','J','P','G'), fps, size) for j in range(i, min(i+20, frame_count)): videoWriter.write(all_frame[j]) videoWriter.release() videoCapture.release()
import numpy as np import cv2 from PIL import ImageGrab fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D') #you can use other codecs as well. vid = cv2.VideoWriter('record.avi', fourcc, 8, (500, 490)) while (True): img = ImageGrab.grab(bbox=(100, 10, 600, 500)) #x, y, w, h img_np = np.array(img) #frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY) vid.write(img_np) cv2.imshow("frame", img_np) key = cv2.waitKey(1) if key == 27: break vid.release() cv2.destroyAllWindows()
def detect_realtime(YoloV3, output_path, input_size=416, show=False, CLASSES=YOLO_COCO_CLASSES, score_threshold=0.3, iou_threshold=0.45, rectangle_colors=''): times = [] vid = cv2.VideoCapture(0) # by default VideoCapture returns float instead of int width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = int(vid.get(cv2.CAP_PROP_FPS)) codec = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter(output_path, codec, fps, (width, height)) # output_path must be .mp4 while True: _, frame = vid.read() try: original_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) original_frame = cv2.cvtColor(original_frame, cv2.COLOR_BGR2RGB) except: break image_frame = image_preprocess(np.copy(original_frame), [input_size, input_size]) image_frame = tf.expand_dims(image_frame, 0) t1 = time.time() pred_bbox = YoloV3.predict(image_frame) t2 = time.time() pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox] pred_bbox = tf.concat(pred_bbox, axis=0) bboxes = postprocess_boxes(pred_bbox, original_frame, input_size, score_threshold) bboxes = nms(bboxes, iou_threshold, method='nms') times.append(t2 - t1) times = times[-20:] print("Time: {:.2f}ms".format(sum(times) / len(times) * 1000)) frame = draw_bbox(original_frame, bboxes, CLASSES=CLASSES, rectangle_colors=rectangle_colors) frame = cv2.putText( frame, "Time: {:.2f}ms".format(sum(times) / len(times) * 1000), (0, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2) if output_path != '': out.write(frame) if show: cv2.imshow('output', frame) if cv2.waitKey(25) & 0xFF == ord("q"): cv2.destroyAllWindows() break cv2.destroyAllWindows()
def main(): if not os.path.isfile(args.face): raise ValueError( '--face argument must be a valid path to video/image file') elif args.face.split('.')[1] in ['jpg', 'png', 'jpeg']: full_frames = [cv2.imread(args.face)] fps = args.fps else: video_stream = cv2.VideoCapture(args.face) fps = video_stream.get(cv2.CAP_PROP_FPS) print('Reading video frames...') full_frames = [] while 1: still_reading, frame = video_stream.read() if not still_reading: video_stream.release() break if args.resize_factor > 1: frame = cv2.resize(frame, (frame.shape[1] // args.resize_factor, frame.shape[0] // args.resize_factor)) if args.rotate: frame = cv2.rotate(frame, cv2.cv2.ROTATE_90_CLOCKWISE) y1, y2, x1, x2 = args.crop if x2 == -1: x2 = frame.shape[1] if y2 == -1: y2 = frame.shape[0] frame = frame[y1:y2, x1:x2] full_frames.append(frame) print("Number of frames available for inference: " + str(len(full_frames))) # if not args.audio.endswith('.wav'): # print('Extracting raw audio...') # command = 'ffmpeg -y -i {} -strict -2 {}'.format(args.audio, 'temp/temp.wav') # subprocess.call(command, shell=True) # args.audio = 'temp/temp.wav' wav = audio.load_wav(args.audio, 16000) mel = audio.melspectrogram(wav) print(mel.shape) if np.isnan(mel.reshape(-1)).sum() > 0: raise ValueError( 'Mel contains nan! Using a TTS voice? Add a small epsilon noise to the wav file and try again' ) mel_chunks = [] mel_idx_multiplier = 80. / fps i = 0 while 1: start_idx = int(i * mel_idx_multiplier) if start_idx + mel_step_size > len(mel[0]): mel_chunks.append(mel[:, len(mel[0]) - mel_step_size:]) break mel_chunks.append(mel[:, start_idx:start_idx + mel_step_size]) i += 1 print("Length of mel chunks: {}".format(len(mel_chunks))) full_frames = full_frames[:len(mel_chunks)] batch_size = args.wav2lip_batch_size gen = datagen(full_frames.copy(), mel_chunks) for i, (img_batch, mel_batch, frames, coords) in enumerate( tqdm(gen, total=int(np.ceil(float(len(mel_chunks)) / batch_size)))): if i == 0: model = load_model(args.checkpoint_path) print("Model loaded") frame_h, frame_w = full_frames[0].shape[:-1] out = cv2.VideoWriter('temp/result.avi', cv2.VideoWriter_fourcc(*'DIVX'), fps, (frame_w, frame_h)) img_batch = torch.FloatTensor(np.transpose(img_batch, (0, 3, 1, 2))).to(device) mel_batch = torch.FloatTensor(np.transpose(mel_batch, (0, 3, 1, 2))).to(device) with torch.no_grad(): pred = model(mel_batch, img_batch) pred = pred.cpu().numpy().transpose(0, 2, 3, 1) * 255. for p, f, c in zip(pred, frames, coords): y1, y2, x1, x2 = c p = cv2.resize(p.astype(np.uint8), (x2 - x1, y2 - y1)) f[y1:y2, x1:x2] = p out.write(f) out.release()
if winner == 'noball' and globalNoBallCounter > th: globalNoBallVideo.append(buffer) if winner == 'out' and globalOutCounter > th: globalOutVideo.append(buffer) if winner == 'six' and globalSixCounter > th: globalSixVideo.append(buffer) if winner == 'wide' and globalWideCounter > th: globalWideVideo.append(buffer) cv2.destroyAllWindows() print('Summarizing Video...') if globalNoBallVideo != []: noBallVideo = cv2.VideoWriter(f'{video_summary_save_path}/no_ball.avi', cv2.VideoWriter_fourcc(*'DIVX'), 25, size) for i in range(len(globalNoBallVideo)): for j in range(len(globalNoBallVideo[i])): # writing to a image array noBallVideo.write(globalNoBallVideo[i][j]) noBallVideo.release() if globalOutVideo != []: outVideo = cv2.VideoWriter(f'{video_summary_save_path}/out.avi', cv2.VideoWriter_fourcc(*'DIVX'), 25, size) for i in range(len(globalOutVideo)): for j in range(len(globalOutVideo[i])): # writing to a image array outVideo.write(globalOutVideo[i][j]) outVideo.release()
"kernel" : np.ones((5,5),np.uint8), "minAreaMask" : 800.0/mainParameters["resize_ROI"], "maxAreaMask" : 8000.0/mainParameters["resize_ROI"], "minDist" : 3.0, "cageLength" : 25., "cageWidth" : 50., }; displayParameters = { "mode" : "v", "showStream" : False, }; savingParameters = { "framerate" : 15, "fourcc" : cv2.VideoWriter_fourcc(*'MJPG'), "mode" : "v", "rawVideoFileName" : "raw_video_mouse", "segVideoFileName" : "seg_video_mouse", "rawWriter" : None, "segWriter" : None, "saveVideo" : False, }; trackerParameters = { "main" : mainParameters, "display" : displayParameters, "saving" : savingParameters, "segmentation" : segmentationParameters, };
def _video_writer(self): self.logger.debug("Entering _video_writer()") self._last_filename = None self._video_codec = cv2.VideoWriter_fourcc( *self._config.camera_fourcc_codec) while self._video_recorder_enabled: while True: self.logger.debug( "Recorder Save Enabled: %s, Video queue empty: %s", self._video_recorder_save, self._video_queue.empty()) try: self.logger.debug( "Attempting to pop video from from queue, queue size is roughly %s", self._video_queue.qsize()) _video_queue_record = self._video_queue.get(False) self._video_queue.task_done() self.logger.debug("Received filename %s", _video_queue_record[0]) if self._last_filename is None: self._last_filename = _video_queue_record[0] self.logger.debug("New filename is %s", self._last_filename) self._video_writer = cv2.VideoWriter( self._config.storage_path + self._last_filename, self._video_codec, self._config.camera_framerate, (self._config.camera_xresolution, self._config.camera_yresolution)) elif not self._last_filename == _video_queue_record[0]: self.logger.debug("New filename is %s", self._last_filename) self._last_filename = _video_queue_record[0] self._video_writer.release() self._video_writer = cv2.VideoWriter( self._config.storage_path + self._last_filename, self._video_codec, self._config.camera_framerate, (self._config.camera_xresolution, self._config.camera_yresolution)) _write_frame = self._video_overlay(_video_queue_record[1], _video_queue_record[2], _video_queue_record[3]) self.logger.debug('Shape of source frame is %s', _write_frame.shape) self._video_writer.write(_write_frame) except: self.logger.debug( 'Entered _video_writer() exception logic') self.logger.debug('*** Last Filename: %s', self._last_filename) if self._last_filename is None: time.sleep(3) break self.logger.debug("Video queue empty") self.logger.debug("Completing video writing") self._video_writer.release() if self._config.enable_azure: cloud_storage_file_path = self._config.storage_path + self._last_filename cs = cloudstorage.CloudStorage(self._config) cs.store_cloud_image(cloud_storage_file_path) self._last_filename = None self._video_writer.release() self.logger.debug("Leaving _video_writer()") time.sleep(3) break
def main(): #not sure whether this is effective or not tf.executing_eagerly() strategy = tf.distribute.OneDeviceStrategy(device="/gpu:0") with strategy.scope(): # if True: #SETTINGS TO ADJUST------------------------------------------- #whether or not to save video to output file or show on screen RECORD = False INPUT_VID = 'aot1' #INPUT_VID = 'mrb3' #INPUT_VID OUTPUT_VID= 'C:/Users/Nikki/Documents/work/inputs-outputs/vid_output/' + INPUT_VID + '.avi' SHOW_VID = True THROWOUT_NUM = 3 #min is 1 INPUT_SIZE = 419 #608 #230 #999 #800 #initialize constants STRIDES = np.array(cfg.YOLO.STRIDES) ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS) NUM_CLASS = len(utils.read_class_names(cfg.YOLO.CLASSES)) XYSCALE = cfg.YOLO.XYSCALE WEIGHTS = './data/yolov4.weights' #must end in .weights #setup variables based on what video is being used video_path, GPS_pix, pix_GPS, origin = pg.sample_select(INPUT_VID) video_path = addresses.TEST #start video capture print("Video from: ", video_path ) vid = cv2.VideoCapture(video_path) #initialize occupancy and compliance buffers buf_size = 5 count_buf = buf_size * [0] ind = 0 people_buf = buf_size * [0] #open file to output to output_f = 'C:/Users/Nikki/Documents/work/inputs-outputs/txt_output/' + INPUT_VID + '.txt' f = open(output_f, 'w') print('file started') f.write('Time\t\t\t\tPed\t<6ft\n') #define writer and output video properties if RECORD: fps = vid.get(5) wdt = int(vid.get(3)) hgt = int(vid.get(4)) fourcc = cv2.VideoWriter_fourcc(*'MJPG') out_vid = cv2.VideoWriter(OUTPUT_VID, fourcc, fps/THROWOUT_NUM, (wdt, hgt)) #generate model input_layer = tf.keras.Input([INPUT_SIZE, INPUT_SIZE, 3]) feature_maps = YOLOv4(input_layer, NUM_CLASS) bbox_tensors = [] for i, fm in enumerate(feature_maps): bbox_tensor = decode(fm, NUM_CLASS, i) bbox_tensors.append(bbox_tensor) model = tf.keras.Model(input_layer, bbox_tensors) print('model built') #force to run eagerly model.run_eagerly = True #load existing weights into model utils.load_weights(model, WEIGHTS) #continue reading and showing frames until interrupted try: while True: #skip desired number of frames to speed up processing for i in range (THROWOUT_NUM): vid.grab() #get current time and next frame dt = str(datetime.datetime.now()) return_value, frame = vid.retrieve() # check that the next frame exists, if not, close display window and exit loop if return_value: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #image = Image.fromarray(frame) else: if SHOW_VID: cv2.destroyWindow('result') print('Video has ended') break #resize image and add another dimension frame_size = frame.shape[:2] cur_frame = np.copy(frame) image_data = utils.image_preprocess(cur_frame, [INPUT_SIZE, INPUT_SIZE]) image_data = image_data[np.newaxis, ...].astype(np.float32) prev_time = time.time() #for calculating how long it takes to process a frame with tf.device('/GPU:0'): image_data = tf.convert_to_tensor(image_data) print(image_data.device) #for calculating how long it takes to process a frame curr_time = time.time() exec_time = curr_time - prev_time info = "time1: %.2f ms" %(1000*exec_time) print(info) prev_time = time.time() #make bboxes pred_bbox = model.predict(image_data) pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES, XYSCALE) all_bboxes, probs, classes = utils.postprocess_boxes(pred_bbox, frame_size, INPUT_SIZE, 0.25)#.25 bboxes = utils.filter_people(all_bboxes, probs, classes) #only continue processing if there were people identified if len(bboxes) > 0: #get rid of redundant boxes bboxes = utils.nms(bboxes, 0.213, method='nms') #.213 #draw bbox and get centered point at base of box frame = utils.draw_bbox(frame, bboxes, show_label = False) pts = utils.get_ftpts(bboxes) #draw radii and count people frame, count_buf[ind] = pg.draw_radius(frame, pts, GPS_pix, pix_GPS, origin) people_buf[ind] = pts.shape[0] else: count_buf[ind] = 0 people_buf[ind] = 0 #avg people and count within 6ft buffers people = int(sum(people_buf)/len(people_buf)) count = int(sum(count_buf)/len(count_buf)) #write info to file and overlay on video utils.video_write_info(f, bboxes, dt, count, people) utils.overlay_occupancy(frame, count, people, frame_size) #for calculating how long it takes to process a frame curr_time = time.time() exec_time = curr_time - prev_time info = "time2: %.2f ms" %(1000*exec_time) print(info) #convert frame to correct cv colors and display/record result = np.asarray(frame) result = cv2.cvtColor(result, cv2.COLOR_RGB2BGR) if SHOW_VID: cv2.namedWindow("result", cv2.WINDOW_NORMAL) cv2.imshow("result", result) if RECORD: out_vid.write(result) if cv2.waitKey(1) & 0xFF == ord('q'): break #increment index ind = (ind + 1) % buf_size #end video, close viewer, stop writing to file vid.release() if RECORD: out_vid.release() if SHOW_VID: cv2.destroyAllWindows() f.close() #if interrupted, end video, close viewer, stop writing to file except: print("Unexpected error:", sys.exc_info()[0]) vid.release() if RECORD == True: out_vid.release() if SHOW_VID: cv2.destroyAllWindows() f.close()
array_boxes_detected[index_pt2][0]), (array_boxes_detected[index_pt2][3], array_boxes_detected[index_pt2][2]), COLOR_RED, 2) # Draw the green rectangle to delimitate the detection zone draw_rectangle(corner_points) # Show both images cv2.imshow("Bird view", bird_view_img) cv2.imshow("Original picture", frame) key = cv2.waitKey(1) & 0xFF # Write the both outputs video to a local folders if output_video_1 is None and output_video_2 is None: fourcc1 = cv2.VideoWriter_fourcc(*"MJPG") output_video_1 = cv2.VideoWriter("../output/video.avi", fourcc1, 25, (frame.shape[1], frame.shape[0]), True) fourcc2 = cv2.VideoWriter_fourcc(*"MJPG") output_video_2 = cv2.VideoWriter( "../output/bird_view.avi", fourcc2, 25, (bird_view_img.shape[1], bird_view_img.shape[0]), True) elif output_video_1 is not None and output_video_2 is not None: output_video_1.write(frame) output_video_2.write(bird_view_img) # Break the loop if key == ord("q"): break
def detect(opt): source, weights, view_img, save_txt, imgsz = opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size save_img = not opt.nosave and not source.endswith( '.txt') # save inference images webcam = source.isnumeric() or source.endswith( '.txt') or source.lower().startswith( ('rtsp://', 'rtmp://', 'http://', 'https://')) # Directories save_dir = increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok) # increment run (save_dir / 'labels' if save_txt else save_dir).mkdir( parents=True, exist_ok=True) # make dir # Initialize set_logging() device = select_device(opt.device) half = device.type != 'cpu' # half precision only supported on CUDA # Load model model = attempt_load(weights, map_location=device) # load FP32 model stride = int(model.stride.max()) # model stride imgsz = check_img_size(imgsz, s=stride) # check img_size if half: model.half() # to FP16 # Second-stage classifier classify = False if classify: modelc = load_classifier(name='resnet101', n=2) # initialize modelc.load_state_dict( torch.load('weights/resnet101.pt', map_location=device)['model']).to(device).eval() # Set Dataloader vid_path, vid_writer = None, None if webcam: view_img = check_imshow() cudnn.benchmark = True # set True to speed up constant image size inference dataset = LoadStreams(source, img_size=imgsz, stride=stride) else: dataset = LoadImages(source, img_size=imgsz, stride=stride) # Get names and colors names = model.module.names if hasattr(model, 'module') else model.names colors = [[random.randint(0, 255) for _ in range(3)] for _ in names] # Run inference if device.type != 'cpu': model( torch.zeros(1, 3, imgsz, imgsz).to(device).type_as( next(model.parameters()))) # run once t0 = time.time() for path, img, im0s, vid_cap in dataset: img = torch.from_numpy(img).to(device) img = img.half() if half else img.float() # uint8 to fp16/32 img /= 255.0 # 0 - 255 to 0.0 - 1.0 if img.ndimension() == 3: img = img.unsqueeze(0) # Inference t1 = time_synchronized() pred = model(img, augment=opt.augment)[0] # Apply NMS pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms) t2 = time_synchronized() # Apply Classifier if classify: pred = apply_classifier(pred, modelc, img, im0s) # Process detections for i, det in enumerate(pred): # detections per image if webcam: # batch_size >= 1 p, s, im0, frame = path[i], '%g: ' % i, im0s[i].copy( ), dataset.count else: p, s, im0, frame = path, '', im0s.copy(), getattr( dataset, 'frame', 0) p = Path(p) # to Path save_path = str(save_dir / p.name) # img.jpg txt_path = str(save_dir / 'labels' / p.stem) + ( '' if dataset.mode == 'image' else f'_{frame}') # img.txt s += '%gx%g ' % img.shape[2:] # print string gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh if len(det): # Rescale boxes from img_size to im0 size det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() # Print results for c in det[:, -1].unique(): n = (det[:, -1] == c).sum() # detections per class s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string # Write results for *xyxy, conf, cls in reversed(det): if save_txt: # Write to file xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh line = (cls, *xywh, conf) if opt.save_conf else ( cls, *xywh) # label format with open(txt_path + '.txt', 'a') as f: f.write(('%g ' * len(line)).rstrip() % line + '\n') if save_img or opt.save_crop or view_img: # Add bbox to image c = int(cls) # integer class label = None if opt.hide_labels else ( names[c] if opt.hide_conf else f'{names[c]} {conf:.2f}') plot_one_box(xyxy, im0, label=label, color=colors[c], line_thickness=opt.line_thickness) if opt.save_crop: save_one_box(xyxy, im0s, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True) # Print time (inference + NMS) print(f'{s}Done. ({t2 - t1:.3f}s)') # Stream results if view_img: cv2.imshow(str(p), im0) cv2.waitKey(1) # 1 millisecond # Save results (image with detections) if save_img: if dataset.mode == 'image': cv2.imwrite(save_path, im0) else: # 'video' or 'stream' if vid_path != save_path: # new video vid_path = save_path if isinstance(vid_writer, cv2.VideoWriter): vid_writer.release( ) # release previous video writer if vid_cap: # video fps = vid_cap.get(cv2.CAP_PROP_FPS) w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) else: # stream fps, w, h = 30, im0.shape[1], im0.shape[0] save_path += '.mp4' vid_writer = cv2.VideoWriter( save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h)) vid_writer.write(im0) if save_txt or save_img: s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else '' print(f"Results saved to {save_dir}{s}") print(f'Done. ({time.time() - t0:.3f}s)')
def analyzeVideo(): onlyFirstFrame = False output = True path = '../Oxford_dataset/stereo/centre/' frame_paths = [] for img in os.listdir(path): frame_paths.append(path + img) frame_paths.sort() fx, fy, cx, cy, G_camera_image, LUT = ReadCameraModel( '../Oxford_dataset/model') K = np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]]) # removes poorly exposed frames for i in range(22): frame_paths.pop(0) surf = cv2.xfeatures2d.SURF_create() FLANN_INDEX_KDTREE = 1 index_params = {'algorithm': FLANN_INDEX_KDTREE, 'trees': 5} search_params = {'checks': 50} # or pass empty dictionary flann = cv2.FlannBasedMatcher(index_params, search_params) prev_img = raw2Undistorted(frame_paths.pop(0), LUT) width = prev_img.shape[1] kp1, des1 = surf.detectAndCompute(prev_img, None) camera_origin = np.array([[0, 0, 0]], dtype='float64').T last_point = camera_origin C0 = np.array([[0, 0, 0]]).T R0 = np.identity(3) bot_row = np.array([0, 0, 0, 1]) T_last = np.hstack((R0, C0)) T_last = np.vstack((T_last, bot_row)) dpi = 300 fig1 = plt.figure() ax1 = fig1.add_subplot(111) canvas1 = FigureCanvas(fig1) ax1.axis('equal') ax1.set_xlabel('X') ax1.set_ylabel('Z') fig2 = plt.figure() ax2 = fig2.add_subplot(111) canvas2 = FigureCanvas(fig2) ax2.axis('equal') ax2.set_xlabel('Y') ax2.set_ylabel('Z') for frame_count, path in enumerate(frame_paths): cur_img = raw2Undistorted(path, LUT) # Find keypoints kp2, des2 = surf.detectAndCompute(cur_img, None) # Match keypoints to find correspondencies matches = flann.knnMatch(des1, des2, k=2) x_a, x_b = [], [] for k in range(len(matches)): x_a.append(kp1[matches[k][0].queryIdx].pt) x_b.append(kp2[matches[k][0].trainIdx].pt) x_a_scaled = convertImageCoordToCenter(x_a, prev_img.shape[1], prev_img.shape[0]) x_b_scaled = convertImageCoordToCenter(x_b, cur_img.shape[1], cur_img.shape[0]) # Find set of inliers using RANSAC F, inliers, mask = inlierRANSAC(x_a_scaled, x_b_scaled, iterations=20) # F,inliers,mask = inlierRANSAC(x_a,x_b,iterations=50) ns_inliers = [] for i, val in enumerate(mask): if val == 1: ns_inliers.append((x_a[i], x_b[i])) match_img = showMatches(prev_img, cur_img, ns_inliers) # Estimate Essential Matrix E = estimateEssentialMatrix(F, K) # Extract 4 Possible Camera Poses Cset, Rset = extractCameraPose(E) # Compute the real world coordinates for each set of matched points Xset = [] for C, R in zip(Cset, Rset): X = LinearTriangulation(K, C0, R0, C, R, inliers) Xset.append(X) # Find which configuration passes the cheirality check C, R = Cheirality(Cset, Rset, Xset) if np.linalg.det(R) < 0: R = -R C = -C # Plot the trajectory T_cur = np.hstack((R, C)) T_cur = np.vstack((T_cur, bot_row)) T_tot = T_last @ T_cur # current_point = last_point + C current_point = T_tot[:, -1] xs = [last_point[0], current_point[0]] ys = [last_point[1], current_point[1]] zs = [last_point[2], current_point[2]] # Visualizer graph1 = plot2image(canvas1, ax1, xs, zs, width) graph2 = plot2image(canvas2, ax2, ys, zs, width) graphs = np.hstack((graph1, graph2)) matchesgraphs = np.vstack((match_img, graphs)) cv2.imshow("Matches and Graphs", matchesgraphs) if output: if frame_count == 0: fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') filename = 'camera tracker.mp4' fps_out = 10 if os.path.exists(filename): os.remove(filename) print('Writing to video. Please Wait.') out = cv2.VideoWriter( filename, fourcc, fps_out, (matchesgraphs.shape[1], matchesgraphs.shape[0])) print('Frame ' + str(frame_count) + ' of ' + str(len(frame_paths))) out.write(matchesgraphs) # change current values to previous values for next loop prev_img = cur_img kp1, des1 = kp2, des2 last_point = current_point T_last = T_tot # cv2.imshow('Frame',cur_img) if (cv2.waitKey(10) == ord('q')): break if onlyFirstFrame: exit() out.release()
def main(): os.makedirs(OUTPUT_DIR, exist_ok=True) config = Config() face_detection_api = FaceDetectionApi(config=config, params=params) #face_ft_extractor = FaceFtExtractorApi(config=config) gender_estimator = GenderDetectionPb(config=config) emo_estimation = ExpressionDetectionPb(config=config) face_sample = "data/test_video/USHER - DEA_000030.jpg" # face_sample = "data/test_video/sample.jpg" #video_pth = "data/test_video/hiv00004.mp4" cam = "cam02" name = "191215_016.mp4" video_pth = "/media/aioz-trung-intern/data/sml/" + cam + "/" + name cap = cv2.VideoCapture(video_pth) vid_w, vid_h = cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get( cv2.CAP_PROP_FRAME_HEIGHT) fps = cap.get(cv2.CAP_PROP_FPS) total_frame = cap.get(cv2.CAP_PROP_FRAME_COUNT) # # INIT WRITER # fourcc = cv2.VideoWriter_fourcc(*'mp4v') # out = cv2.VideoWriter(OUT_VIDEO_PATH, fourcc, fps, (int(vid_w), int(vid_h))) # print("[INFO] information: ", vid_w, vid_h, fps) count = 0 # GET SAMPLE: # img_sample = cv2.imread(face_sample) # img_sample = cv2.cvtColor(img_sample, cv2.COLOR_BGR2RGB) # img_sample = np.transpose(img_sample, (2, 0, 1)) # face_ft_sample = face_ft_extractor.proceed(img_sample) emo_s = np.zeros(2) list_emo = ["Hap", "Sad"] # Face list face_list = [Face_List(), Face_List()] # for i in range(4): # tmp = Face_List() # face_list.append(tmp) # Load mask mask_name = "mask_cam02_4.jpg" mask = cv2.imread(mask_name, 0) #mask_region = [[(534, 401), (788, 626)], [(803, 336), (1071, 575)], [(1356, 266), (1553, 437)], [(1559, 258), (1754, 402)]] #(mask_cam02_2) mask_region = [ [(540, 420), (667, 549)], [(798, 411), (954, 558)] ] #, [(1419, 301), (1570, 453)], [(1575, 261), (1764, 438)]] #(mask_cam02_3) mask_box = [(479, 386), (1175, 1077)] time_to_live = 450 no_face_count = [time_to_live] * 2 # After this period, if no face is regconized, face will be count as new one #is_update = False fourcc = cv2.VideoWriter_fourcc(*'MP4V') out = cv2.VideoWriter('result.mp4', fourcc, 30, (int(vid_w), int(vid_h))) while cap.isOpened and count <= 600 * 30: ret, frame = cap.read() if ret: # CROP frame org_frame = frame.copy() cv2.putText(org_frame, 'Current Emotion', (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 255), 3) cv2.rectangle(org_frame, mask_box[0], mask_box[1], (255, 0, 0), 2) frame = cv2.bitwise_and(frame, frame, mask=mask) # Draw mask # for i in range(len(face_list)): # cv2.rectangle(org_frame, mask_region[i][0], mask_region[i][1], (255,0,0), 2) # DETECTION boxes, faces, elapsed = face_detection_api.proceed(frame=frame, vid_w=vid_w, vid_h=vid_h) check_mask = [False] * len(face_list) if boxes is not None: sim = [] for box, face in zip(boxes, faces): # Detect face gender and emotion org_face = face.copy() emo, gen = detect_face_information(org_face, emo_estimation, gender_estimator) # Print face gender and emotion if emo in list_emo: idx = list_emo.index(emo) emo_s[idx] += 1 emo_percent = emo_s / np.sum(emo_s) emo_percent = np.around(emo_percent, decimals=2) if count > 10: emo_str = emo else: emo_str = '' # gen = gender_estimator.process_prediction(face, use_tta=True) gen_str = "M" if gen > 0.5 else "F" vis(image=org_frame, bbox=box, name="DAE", gen=gen_str, emo=emo_str) # Assign Face in ROI for i in range(len(face_list)): print(mask_region[i], box) if (check_in_mask(mask_region[i], box)): check_mask[i] = True # New face in region if (no_face_count[i] >= time_to_live): face_list[i].add_new_face( org_face, None, emo, count) # Old face in region else: face_list[i].duration_container[-1].append( count) face_list[i].emotion_container[-1].append(emo) no_face_count[i] = 0 break # Extract face's feature # face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) # face = np.transpose(face, (2, 0, 1)) # face_ft = face_ft_extractor.proceed(face) # # Check new face # flag = False # for i in range(face_list.num_of_face): # # Calculating face similarity # _sim = np.dot(face_list.feature_container[i], face_ft.T) # if (_sim >= 0.2): # flag=True # face_list.duration_container[i].append(count) # face_list.emotion_container[i].append(emo) # break # if (not flag): # face_list.add_new_face(org_face, face_ft, emo, count) #is_update = True # CALCULATING similarity # _sim = np.dot(face_ft_sample, face_ft.T) # sim.append(_sim) # sim = np.asarray(sim) # indices = np.argmax(sim) # print(sim[indices]) # FACE REGCONITION, GENDER and EMOTION DETECTION # face = faces[0] # face = cv2.cvtColor(face, cv2.COLOR_RGB2GRAY) # face = cv2.cvtColor(face, cv2.COLOR_GRAY2BGR) # emo = emo_estimation.process_prediction(face)[0] for i in range(len(face_list)): if (not check_mask[i]): no_face_count[i] += 1 # print("Frame: {}, Time: {}".format(count, elapsed)) out.write(org_frame) org_frame = cv2.resize(org_frame, (int(vid_w // 2), int(vid_h // 2))) cv2.imshow("abc", org_frame) count += 1 if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() for i in range(len(face_list)): face_list[i].save_list(i)
import pandas as pd from pandas import Series, DataFrame import numpy as np import argparse parser = argparse.ArgumentParser(description="Find contours and centroids") parser.add_argument("input", help="Source video") args = parser.parse_args() # video capture (reader) cap = cv2.VideoCapture(args.input) cv2.namedWindow("frame", cv2.WINDOW_NORMAL) cv2.resizeWindow("frame", 800, 600) # video writer fourcc = cv2.VideoWriter_fourcc(*"MP4V") h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) fps = cap.get(cv2.CAP_PROP_FPS) out = cv2.VideoWriter("video_" + args.input + ".mp4", fourcc, fps, (w, h)) # kernels erosion_kernel = np.ones((5, 5), np.uint8) dilation_kernel = np.ones((5, 5), np.uint8) # blob detection ================================ # Setup SimpleBlobDetector parameters. params = cv2.SimpleBlobDetector_Params() # Change thresholds params.minDistBetweenBlobs = 0 params.filterByColor = True
return min_dist, door_open database = {} for face in faces: database[face] = [] for face in faces: for img in os.listdir(paths[face]): database[face].append(img_to_encoding(os.path.join(paths[face],img), FRmodel)) camera = cv2.VideoCapture(0) fd = faceDetector('fd_models/haarcascade_frontalface_default.xml') fourcc = cv2.VideoWriter_fourcc(*'XVID') #codec for video out = cv2.VideoWriter('output.avi', fourcc, 20, (800, 600) )#Output object while True: ret, frame = camera.read() frame = imutils.resize(frame, width = 800) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #print(frame.shape) faceRects = fd.detect(gray) for (x, y, w, h) in faceRects: roi = frame[y:y+h,x:x+w] roi = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB) roi = cv2.resize(roi,(IMAGE_SIZE, IMAGE_SIZE)) min_dist = 1000 identity = "" detected = False
def main(args): model = args.model device = args.device video_file = args.video max_people = args.max_people threshold = args.threshold output_path = args.output_path start_model_load_time = time.time() pd = PersonDetect(model, device, threshold) pd.load_model() total_model_load_time = time.time() - start_model_load_time queue = Queue() try: queue_param = np.load(args.queue_param) for q in queue_param: queue.add_queue(q) except: print("error loading queue param file") try: cap = cv2.VideoCapture(video_file) except FileNotFoundError: print("Cannot locate video file: " + video_file) except Exception as e: print("Something else went wrong with the video file: ", e) initial_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) initial_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) video_len = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) fps = int(cap.get(cv2.CAP_PROP_FPS)) out_video = cv2.VideoWriter(os.path.join(output_path, 'output_video.mp4'), cv2.VideoWriter_fourcc(*'avc1'), fps, (initial_w, initial_h), True) counter = 0 start_inference_time = time.time() try: while cap.isOpened(): ret, frame = cap.read() if not ret: break counter += 1 coords, image = pd.predict(frame) num_people = queue.check_coords(coords) print(f"Total People in frame = {len(coords)}") print(f"Number of people in queue = {num_people}") out_text = "" y_pixel = 25 for k, v in num_people.items(): out_text += f"No. of People in Queue {k} is {v} " if v >= int(max_people): out_text += f" Queue full; Please move to next Queue " cv2.putText(image, out_text, (15, y_pixel), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2) out_text = "" y_pixel += 40 out_video.write(image) total_time = time.time() - start_inference_time total_inference_time = round(total_time, 1) fps = counter / total_inference_time with open(os.path.join(output_path, 'stats.txt'), 'w') as f: f.write(str(total_inference_time) + '\n') f.write(str(fps) + '\n') f.write(str(total_model_load_time) + '\n') cap.release() cv2.destroyAllWindows() except Exception as e: print("Could not run Inference: ", e)
def run( weights=ROOT / 'yolov5s.pt', # model.pt path(s) source=ROOT / 'data/images', # file/dir/URL/glob, 0 for webcam data=ROOT / 'data/coco128.yaml', # dataset.yaml path imgsz=(640, 640), # inference size (height, width) conf_thres=0.25, # confidence threshold iou_thres=0.45, # NMS IOU threshold max_det=1000, # maximum detections per image device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu view_img=False, # show results save_txt=False, # save results to *.txt save_conf=False, # save confidences in --save-txt labels save_crop=False, # save cropped prediction boxes nosave=False, # do not save images/videos classes=None, # filter by class: --class 0, or --class 0 2 3 agnostic_nms=False, # class-agnostic NMS augment=False, # augmented inference visualize=False, # visualize features update=False, # update all models project=ROOT / 'runs/detect', # save results to project/name name='exp', # save results to project/name exist_ok=False, # existing project/name ok, do not increment line_thickness=3, # bounding box thickness (pixels) hide_labels=False, # hide labels hide_conf=False, # hide confidences half=False, # use FP16 half-precision inference dnn=False, # use OpenCV DNN for ONNX inference ): source = str(source) save_img = not nosave and not source.endswith( '.txt') # save inference images is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS) is_url = source.lower().startswith( ('rtsp://', 'rtmp://', 'http://', 'https://')) webcam = source.isnumeric() or source.endswith('.txt') or (is_url and not is_file) if is_url and is_file: source = check_file(source) # download # Directories save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run (save_dir / 'labels' if save_txt else save_dir).mkdir( parents=True, exist_ok=True) # make dir # Load model device = select_device(device) model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data) stride, names, pt, jit, onnx, engine = model.stride, model.names, model.pt, model.jit, model.onnx, model.engine imgsz = check_img_size(imgsz, s=stride) # check image size # Half half &= ( pt or jit or onnx or engine ) and device.type != 'cpu' # FP16 supported on limited backends with CUDA if pt or jit: model.model.half() if half else model.model.float() # Dataloader if webcam: view_img = check_imshow() cudnn.benchmark = True # set True to speed up constant image size inference dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt) bs = len(dataset) # batch_size else: dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt) bs = 1 # batch_size vid_path, vid_writer = [None] * bs, [None] * bs # Run inference model.warmup(imgsz=(1, 3, *imgsz), half=half) # warmup dt, seen = [0.0, 0.0, 0.0], 0 for path, im, im0s, vid_cap, s in dataset: t1 = time_sync() im = torch.from_numpy(im).to(device) im = im.half() if half else im.float() # uint8 to fp16/32 im /= 255 # 0 - 255 to 0.0 - 1.0 if len(im.shape) == 3: im = im[None] # expand for batch dim t2 = time_sync() dt[0] += t2 - t1 # Inference visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False pred = model(im, augment=augment, visualize=visualize) t3 = time_sync() dt[1] += t3 - t2 # NMS pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det) dt[2] += time_sync() - t3 # Second-stage classifier (optional) # pred = utils.general.apply_classifier(pred, classifier_model, im, im0s) # Process predictions for i, det in enumerate(pred): # per image seen += 1 if webcam: # batch_size >= 1 p, im0, frame = path[i], im0s[i].copy(), dataset.count s += f'{i}: ' else: p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0) p = Path(p) # to Path save_path = str(save_dir / p.name) # im.jpg txt_path = str(save_dir / 'labels' / p.stem) + ( '' if dataset.mode == 'image' else f'_{frame}') # im.txt s += '%gx%g ' % im.shape[2:] # print string gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh imc = im0.copy() if save_crop else im0 # for save_crop annotator = Annotator(im0, line_width=line_thickness, example=str(names)) if len(det): # Rescale boxes from img_size to im0 size det[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0.shape).round() # Print results for c in det[:, -1].unique(): n = (det[:, -1] == c).sum() # detections per class s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string # Write results for *xyxy, conf, cls in reversed(det): if save_txt: # Write to file xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format with open(txt_path + '.txt', 'a') as f: f.write(('%g ' * len(line)).rstrip() % line + '\n') if save_img or save_crop or view_img: # Add bbox to image c = int(cls) # integer class label = None if hide_labels else ( names[c] if hide_conf else f'{names[c]} {conf:.2f}') annotator.box_label(xyxy, label, color=colors(c, True)) if save_crop: save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True) # Print time (inference-only) LOGGER.info(f'{s}Done. ({t3 - t2:.3f}s)') # Stream results im0 = annotator.result() if view_img: cv2.imshow(str(p), im0) cv2.waitKey(1) # 1 millisecond # Save results (image with detections) if save_img: if dataset.mode == 'image': cv2.imwrite(save_path, im0) else: # 'video' or 'stream' if vid_path[i] != save_path: # new video vid_path[i] = save_path if isinstance(vid_writer[i], cv2.VideoWriter): vid_writer[i].release( ) # release previous video writer if vid_cap: # video fps = vid_cap.get(cv2.CAP_PROP_FPS) w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) else: # stream fps, w, h = 30, im0.shape[1], im0.shape[0] save_path += '.mp4' vid_writer[i] = cv2.VideoWriter( save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h)) vid_writer[i].write(im0) # Print results t = tuple(x / seen * 1E3 for x in dt) # speeds per image LOGGER.info( f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {(1, 3, *imgsz)}' % t) if save_txt or save_img: s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else '' LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}{s}") if update: strip_optimizer(weights) # update model (to fix SourceChangeWarning)
def AnalyzeAndSaveVideo(net, videoFile): # Define the codec and create VideoWriter object (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.') #print major_ver #print minor_ver if net is None: net = setupNet() #writer = VideoWriter("/home/icarus/projects/AgeGender/videos/output.mp4", frameSize=(360, 640)) #writer.open() #Didn'r do this image = numpy.zeros((h, w, 3)) if videoFile is None: videoFile = "/home/icarus/projects/AgeGender/videos/6323.mp4" fourcc = cv2.VideoWriter_fourcc(*'AVCI') out = cv2.VideoWriter('/home/icarus/projects/AgeGender/videos/output.mp4', fourcc, 1.0, (640, 480)) faceCascade = "/home/icarus/projects/AgeGender/cgi-bin/haarcascade_frontalface_default.xml" HOW_MANY_FRAMES_TO_SKIP_FROM_CURRENT_FRAME = 20 #videoFile = "/home/icarus/projects/AgeGender/videos/FB7LoFgv5cA.mp4" cascade = cv2.CascadeClassifier(faceCascade) try: cap = skvideo.io.VideoCapture(videoFile) except: print "problem opening input stream " + videoFile sys.exit(1) if not cap.isOpened(): print "capture stream not open " + videoFile sys.exit(1) frameId = 0 nextFrameId = 0 + 10 startTime = time.time() while(cap.isOpened()): ret, frame = cap.read() frameId = frameId + 1 if (frameId < nextFrameId): continue nextFrameId = frameId + HOW_MANY_FRAMES_TO_SKIP_FROM_CURRENT_FRAME if(frame is None): #print "Frame is of type None. Breaking loop" continue gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = cascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE ) if (len(faces) > 0): print "\t\t\t\t Faces : " + str(len(faces)) for i, f in enumerate(faces): x, y, w, h = [v for v in f] cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255)) frame = frame / 255. input_image = frame[:, :, (2, 1, 0)] sub_face = input_image[y:y + h, x:x + w] s = time.time() emotion = net["emotion"].predict([sub_face], oversample=False) e = time.time() print "Emotion: " + str(e-s) s = time.time() age = net["age"].predict([sub_face]) e = time.time() print "Age: " + str(e-s) s = time.time() gender = net["gender"].predict([sub_face]) e=time.time() print "Gender: " + str(e-s) s = time.time() emotion = categories[emotion.argmax()] age = age_list[age[0].argmax()] gender = gender_list[gender[0].argmax()] text = emotion + " : " + age + " : " + gender e = time.time() print "\t\t\t\t Face: " + str(i) + " has: " + text + " " + "Frame: " + str(frameId) font = cv2.FONT_HERSHEY_SIMPLEX x = x + w # position of text y = y + w # position of text cv2.putText(frame, text, (x, y), font, 6, (200, 255, 155), 13, cv2.LINE_AA) s = time.time() out.write(frame) e = time.time() print "Write to file: " + str(e-s) print "\t\t\t\t Face pisitons: X " + str(x) + " Y: " + str(y) print "Done processing. Releasing Video Stream!!" cap.release()
''' Created on 25 de fev de 2020 Marcelo Queiroz de Lima Brilhante @author: lab ''' from __future__ import print_function import cv2 as cv import numpy as np import argparse #backSub = cv.createBackgroundSubtractorMOG2() backSub = cv.createBackgroundSubtractorKNN(history=9000, dist2Threshold=5000.0, detectShadows=False) out = cv.VideoWriter('outpy.avi', cv.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (720, 576)) capture = cv.VideoCapture('c:/sea.avi') retteste, frameteste = capture.read() #print(frameteste) #print(retteste) #backgroundImage=cv.BackgroundSubtractor.getBackgroundImage(capture) print(type(capture.read)) print(type(retteste)) fgMaskteste = backSub.apply(frameteste) print(fgMaskteste) M = cv.moments(fgMaskteste) cY = int(M["m10"] / M["m00"]) cX = int(M["m01"] / M["m00"]) i = 0 area = 1
# apply non-maxima suppression to suppress weak, overlapping bounding boxes indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold) if len(indices) > 0: for i in indices.flatten(): # extract the bounding box coordinates (x, y) = (boxes[i][0], boxes[i][1]) (w, h) = (boxes[i][2], boxes[i][3]) # draw a bounding box rectangle and label on the image draw_pred(frame, classIDs[i], confidences[i], round(x), round(y), round(x + w), round(y + h)) if writer is None: # initialize our video writer fourcc = cv2.VideoWriter_fourcc(*"MJPG") writer = cv2.VideoWriter('videos/video1_output.avi', fourcc, 30, (frame.shape[1], frame.shape[0]), True) if total > 0: elap = (end - start) print("[INFO] single frame took {:.4f} seconds".format(elap)) print("[INFO] estimated total time to finish: {:.4f}".format( elap * total)) # write the output frame to disk writer.write(frame) print("[INFO] cleaning up...") writer.release() videoStream.release()
from importlib import import_module import os from flask import Flask, render_template, Response import cv2 import numpy as np from time import sleep from usb_camera import UsbCamera app = Flask(__name__) camera = UsbCamera() camera.set_source(0) start_record_video = False record_video = False record_file_name = "" video_writer = cv2.VideoWriter("./vids/empty.avi", cv2.VideoWriter_fourcc(*'XVID'), 20.0, (640, 480)) @app.route('/') def index(): """Video streaming home page.""" return render_template('index.html') @app.route('/getmethod/<jsdata>') def save_frame_1(jsdata): cv2.imwrite("./frames/" + str(jsdata) + ".png", camera.current_frame) return jsdata
import cv2 import numpy as np # 캠으로부터 데이터 가져오기 cap = cv2.VideoCapture(0) if cap.isOpened() == False: print("Unable to read camer feed") else: # 프레임의 정보 가져오기 : 화면크기 ( width, height) frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) ot = cv2.VideoWriter('data/videos/output.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (frame_width, frame_height)) # 캠으로 부터 사진을 계속 입력 받는다. while True: ret, frame = cap.read() if ret == True: out.write(frame) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == 27: break else:
sec = sec + frameRate sec = round(sec, 2) success = getFrame(sec) pathIn = '/Users/prachis/pet_projects/YOLOv2_keras/out/' pathOut = '/Users/prachis/pet_projects/YOLOv2_keras/video.avi' fps = 0.5 frame_array = [] files = [f for f in os.listdir(pathIn) if isfile(join(pathIn, f))] # for sorting the file names properly files.sort(key=lambda x: x[5:-4]) files.sort() frame_array = [] files = [f for f in os.listdir(pathIn) if isfile(join(pathIn, f))] # for sorting the file names properly files.sort(key=lambda x: x[5:-4]) for i in range(len(files)): filename = pathIn + files[i] # reading each files img = cv2.imread(filename) height, width, layers = img.shape size = (width, height) # inserting the frames into an image array frame_array.append(img) out = cv2.VideoWriter(pathOut, cv2.VideoWriter_fourcc(*'DIVX'), fps, size) for i in range(len(frame_array)): # writing to a image array out.write(frame_array[i]) out.release()
import numpy as np import cv2 import time # Getting the face detection xml. This file is found directly in the cv2 (opencv) github repo face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # Grabbing the video capture methods. Note, 0 worked for me instead of 1. cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG")) print("Detecting Pics") start = time.time() counter = 1 while 1: # Detecting and processing pics ret, img = cap.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in faces: img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) img = img[y:y+h, x:x+w] counter += 1 print("Frame Rate: " + str(counter/(time.time() - start)))
""" import cv2, sys if len(sys.argv) < 2: print("Usage videocrop.py <sourcevid> <destvid>") videoCapture = cv2.VideoCapture(sys.argv[1]) # get video properties fps = videoCapture.get(cv2.CAP_PROP_FPS) size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))) # open video writer with same properties as video input videoWriter = cv2.VideoWriter(sys.argv[2], cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), fps, size) ret, frame = videoCapture.read() write = False while (ret): cv2.imshow('OpenCV Video Capture', frame) keycode = cv2.waitKey(150) & 0xff if keycode == ord('q'): break # change writing status if keycode == ord('c'): write = not write # fill video output with frames if write: