python中怎么给编好的程序加一个含有按钮的界面,按下按钮开始执行。再来一个结束执行的按钮
#-*-coding:utf-8-*-importnumpyasnpimportcv2fromimutils.object_detectionimportnon_max_...
# -*- coding:utf-8 -*-
import numpy as np
import cv2
from imutils.object_detection import non_max_suppression
import os
def svmdetectperson(img):
'''
用HOG特征和svm来识别行人
'''
hog=cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
person,w= hog.detectMultiScale(img)
return person
def is_inside(a,b):
'''
判断框的包含关系
'''
x1,y1,w1,h1=a
x2,y2,w2,h2=b #judge b is not include a
return x1>x2 and y1>y2 and x1+w1<x2+w2 and y1+h1<y2+h2
def draw(img,a):
'''
画框
'''
x,y,w,h=a
cv2.rectangle(img,(x,y),(x+w,y+h),(0, 255, 0),2)
# 加载视频
cap = cv2.VideoCapture('record.mp4')
# 当打开视频时操作
while(cap.isOpened()):
# 读取视频每一帧
ret, frame = cap.read()##ret返回布尔量
# 检测每一帧行人
person=svmdetectperson(frame)
# 得到每一帧行人的框
filtered=[]
for i,p in enumerate(person):
for j,p1 in enumerate(person):
if i!=j and is_inside(p,p1):
break
filtered.append(p)
# 用非最大值抑制的方法优化结果
if len(filtered) != 0:
filtered = np.array(filtered, dtype=np.float32)
filtered = non_max_suppression(filtered, probs=None, overlapThresh=0.65)
# 画出结果
for p in filtered:
draw(frame,p)
# 输出每帧检测的人数
print("person number:",len(filtered))
# 展示图片
cv2.imshow("frame",frame)
if cv2.waitKey(1) & 0xFF == ord('p'):
os.system("pause")
#time.sleep("second")
# 按q时退出视频检测
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 结束程序,关闭所有窗口
cap.release()
cv2.destroyAllWindows() 展开
import numpy as np
import cv2
from imutils.object_detection import non_max_suppression
import os
def svmdetectperson(img):
'''
用HOG特征和svm来识别行人
'''
hog=cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
person,w= hog.detectMultiScale(img)
return person
def is_inside(a,b):
'''
判断框的包含关系
'''
x1,y1,w1,h1=a
x2,y2,w2,h2=b #judge b is not include a
return x1>x2 and y1>y2 and x1+w1<x2+w2 and y1+h1<y2+h2
def draw(img,a):
'''
画框
'''
x,y,w,h=a
cv2.rectangle(img,(x,y),(x+w,y+h),(0, 255, 0),2)
# 加载视频
cap = cv2.VideoCapture('record.mp4')
# 当打开视频时操作
while(cap.isOpened()):
# 读取视频每一帧
ret, frame = cap.read()##ret返回布尔量
# 检测每一帧行人
person=svmdetectperson(frame)
# 得到每一帧行人的框
filtered=[]
for i,p in enumerate(person):
for j,p1 in enumerate(person):
if i!=j and is_inside(p,p1):
break
filtered.append(p)
# 用非最大值抑制的方法优化结果
if len(filtered) != 0:
filtered = np.array(filtered, dtype=np.float32)
filtered = non_max_suppression(filtered, probs=None, overlapThresh=0.65)
# 画出结果
for p in filtered:
draw(frame,p)
# 输出每帧检测的人数
print("person number:",len(filtered))
# 展示图片
cv2.imshow("frame",frame)
if cv2.waitKey(1) & 0xFF == ord('p'):
os.system("pause")
#time.sleep("second")
# 按q时退出视频检测
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 结束程序,关闭所有窗口
cap.release()
cv2.destroyAllWindows() 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询