我正在做一个项目,可以识别设备上的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()
现在,输出的是在dtected数字上绘制矩形的实时视频。 这是我上传到youtube上的输出视频的链接
视频livestream不是平滑的并且正在停止,我想知道每秒有多少帧正在被处理,这样我就可以使输出livestream平滑
我尝试了手动功能来计算帧数
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对象,我不知道如何继续计数帧
在这种情况下
请帮忙
如果有办法使新蒸汽更平滑,也请让我知道
谢谢你
测量fps的方法在这个问题中描述:fps - how to divide count by time function to determine fps
为了使视频更平滑,请尝试在canny和gauassian过滤器之前调整图像大小,以降低计算成本,这一点不会丢失太多信息
您可以尝试另一种边缘检测算法,如拉普拉斯算子或sobel滤波器,您也可以使用高通滤波器(中心为正值,外围为负系数),这些滤波器牺牲精度以换取更好的时间。最后,我建议用C++实现更快的处理。p>
哇,我看这不可能顺利进行。你需要完全重构你的代码;并优先使用本地摄像机。此外,matchTemplate可能不是模式识别的好选择,因为它对所有变换和噪声都非常敏感,而且速度非常慢
要测量fps,请将其放置在文件的开头
这是一个接近opencv的消息分派回调
相关问题 更多 >
编程相关推荐