# -*- encoding: utf-8 -*- import cv2 import os import tkinter as tk from tkinter import filedialog def roi(event, x, y, flags, param): img2 = save_img if event == cv2.EVENT_LBUTTONDOWN: # 左键点击,选择点 pts.append((x, y)) # exit(0) if len(pts) > 0: # 将pts中的最后一点画出来 cv2.circle(img2, pts[-1], 3, (0, 0, 255), -1) if len(pts) > 1: # 画线 for i in range(len(pts) - 1): cv2.circle(img2, pts[i], 5, (0, 0, 255), -1) # x ,y 为鼠标点击地方的坐标 cv2.line(img=img2, pt1=pts[i], pt2=pts[i + 1], color=(255, 0, 0), thickness=2) cv2.imshow('image', img2) if __name__=='__main__': root = tk.Tk() root.withdraw() video_path = filedialog.askopenfilename(filetypes=[("Video files", "*.mp4 *.avi *.mov")]) # video_path= 'video/car.MP4' print('-------------------------------------------------------------------') print(''' 运行该程序,视频开始播放 点击键盘空格暂停播放进入编辑界面划定区域 进入编辑页面之后: 1 左键点击选点 2 选两个点之后,自动退出编辑页面 ''') print('-------------------------------------------------------------------') # pts_list = []#用于存放区域集合 pts = [] # 用于存放点坐标 save_img=None#用于保存暂停播放时那的一帧 video=cv2.VideoCapture(video_path) while video.isOpened(): success,img=video.read() if success: cv2.namedWindow('Image',cv2.WINDOW_NORMAL) cv2.imshow('Image',img) save_img=img else: print('+++++++') break if cv2.waitKey(1) & 0xFF == ord(' '): # cv2.waitKey(0) save_img=img break video.release() cv2.destroyAllWindows() cv2.namedWindow('image',cv2.WINDOW_NORMAL) cv2.setMouseCallback('image', roi) cv2.imshow('image',save_img) while True: if len(pts)==2: break key = cv2.waitKey(1) & 0xFF if key == 27: break cv2.destroyAllWindows() print(pts) if len(pts)==2: # 构建命令字符串 # region1=pts_list[0] # region2=pts_list[1] # region1_str='"'+str(region1)+'"' # region2_str='"'+str(region2)+'"' line_str='"'+str(pts)+'"' command = 'python -u people_count.py --file_path ' +video_path+' '+'--line '+ line_str # print(command) os.system(command) else: # print(len(pts_list)) print('选定的点不是两个,无法连成一条直线,无法执行后续程序,请仔细阅读运行须知中划定区域的具体步骤!') exit(0)