python - 跟踪实时 Numpy 视频的帧速率
问题描述
我正在开发一个可以识别设备上 7 个段号的项目。
这是我的代码
import cv2
import numpy as np
from matplotlib import pyplot as plt
import urllib
from urllib import request
import imutils
#I used this to connect the webcam app on the tablet
url='http://192.168.0.121:8080/shot.jpg'
tW=52
tH=98
while True:
imgResp = urllib.request.urlopen(url)
imgNp = np.array(bytearray(imgResp.read()),dtype=np.uint8)
frame = cv2.imdecode(imgNp,-1)
height, width = frame.shape[:2]
top_left_x = int (width / 4)
top_left_y = int ((height / 2) + (height / 6))
bottom_right_x = int ((width / 4) +(width/5))
bottom_right_y = int ((height / 2) - (height / 55))
cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y), 255, 3)
cv2.imshow('user_window',frame)
cropped = frame[bottom_right_y:top_left_y , top_left_x:bottom_right_x]
cv2.imshow('cropped',cropped)
gray = cv2.cvtColor(cropped, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
canny = cv2.Canny(blur, 10, 70)
ret, mask = cv2.threshold(canny, 70, 255, cv2.THRESH_BINARY)
cv2.imshow('mask',mask)
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED']
found=None
detected_number=None
for meth in methods:
print(meth)
for a in range(0,10):
templatepath = ('C:\\Users\\USER\\Desktop\\test\\verniersegment\\'+str(a)+'vernier.png') # trainImage
template = cv2.imread(templatepath,1) # trainImage
print('비교하는 숫자',a)
template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
templateBinary = cv2.threshold(template, 84, 255, cv2.THRESH_BINARY)[1]
template = cv2.Canny(templateBinary, 50, 200)
(tH, tW) = template.shape[:2]
cv2.imshow("Template", template)
for scale in np.linspace (0.2,1.0,num=10)[::-1]:
resized=imutils.resize(mask,width=int(mask.shape[1]*scale) )
r=float(resized.shape[1]/mask.shape[1])
cv2.imshow('resized',resized)
if resized.shape[0]<tH or resized.shape[1]<tW:
print("비교이미지가 template보다 작아짐 ")
break
method=eval(meth)
#matchtemplate함수로 사이즈가 바뀐 부분 ,template을 method방식으로 비교한다
threshold = 0.8
compare = cv2.matchTemplate(resized,template,method)
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(compare)
clone = np.dstack([resized, resized, resized])
#cv2.rectangle(clone, (maxLoc[0], maxLoc[1]),(maxLoc[0] + tW, maxLoc[1] + tH), (0, 0, 255), 2)
if found is None or maxVal > found[0]:
maxVal2=maxVal
found = (maxVal2, maxLoc, r,a)
maxLoc=found[1]
r=found[2]
(startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
(endX, endY) = (int((maxLoc[0] + tW) * r), int((maxLoc[1] + tH) * r))
if maxVal2>7000000:
cv2.rectangle(mask, (startX, startY), (endX, endY), (255,0, 0), 2)
if maxVal2>5000000:
cv2.putText(mask,str(found[3]),(startX+10,startY+10), cv2.FONT_HERSHEY_COMPLEX, 2 ,(255,0,0), 2)
cv2.imshow("Finalized", mask)
if cv2.waitKey(1) == 13:
break
cv2.destroyAllWindows()
现在,输出是在检测到的数字上绘制矩形的实时视频。这是我在 youtube 上上传的输出视频的链接,以便在此处发布。
视频直播不流畅,不流畅,想知道一秒钟处理了多少帧,这样才能让输出的直播流畅。
我尝试了手动功能来计算帧数
def count_frames_manual(video):
# initialize the total number of frames read
total = 0
# loop over the frames of the video
while True:
# grab the current frame
(grabbed, frame) = video.read()
# check to see if we have reached the end of the
# video
if not grabbed:
break
# increment the total number of frames read
total += 1
return total
# return the total number of frames in the
print(count_frames_manual(mask))
但是当我运行该函数时,出现以下错误
AttributeError Traceback (most recent call last)
<ipython-input-9-dd97690639f7> in <module>
----> 1 print(count_frames_manual(mask))
<ipython-input-8-ab227eb24e08> in count_frames_manual(video)
5 while True:
6 # grab the current frame
----> 7 (grabbed, frame) = video.read()
8
9 # check to see if we have reached the end of the
AttributeError: 'numpy.ndarray' object has no attribute 'read'
似乎mask
是一个 NumPy 对象,我不确定在这种情况下如何继续计算帧数。
请帮忙。
如果有办法让直播更流畅,也请告诉我!
提前谢谢!
解决方案
哇,我认为这不可能顺利进行。您需要完全重构您的代码;并优先使用本地摄像头。此外,matchTemplate 可能不是模式识别的好选择,因为它对所有转换和噪声非常敏感,而且速度非常慢。
要测量 fps,请将其放在文件的开头
import time
fps = 0
tau = time.time()
这在 opencv 消息调度回调附近
now = time.time()
if now > tau: # avoid div0
fps = (fps*9 +1/(now-tau))/10
tau = now
print(fps)
if cv2.waitKey(1) == 13:
break
推荐阅读
- javascript - Google 将地址自动填充到表单中
- javascript - 如何禁用存储在 mysql 数据库中的那些日期(在 jquery datepicker 中)?
- php - 通过 HTTP 拦截器添加 XSRF-TOKEN 会导致 400 错误请求
- r - ggplot2中的点颜色和构面
- java - 使用共享首选项更改字体样式
- java - 从字符串返回单个单词的方法
- vb.net - VB以编程方式拉伸DataGridViewImageColumn图像
- java - 如何使用 safari 技术预览执行 selenium 自动化脚本(java)?
- c++ - 这些整数是否未对齐,我什至应该关心吗?
- python - 无法安装plotly python