实时网络摄像头源上的 OCR:图像高度为零,IHDR 数据无效



所以我对openCV和谷歌视觉很陌生,但我正在尝试使用边缘检测来识别标签特定区域中的数字。当标签在相机视野中时,代码运行并工作正常,但是当标签不在视野中时,我收到 libpng 警告:IDHR 中的图像高度为零,libpng 错误:IHDR 数据无效

我试过检查帧是否不是 Nonetype 并且 ret 是 True,但我无法弄清楚如何让它在帧中有一个标签之前不输出任何内容。

下面是示例帧的图像和我的边缘测量的输出 https://i.stack.imgur.com/tVHFm.jpg https://i.stack.imgur.com/Mb3Z5.jpg

import io
import cv2
from PIL import Image
import numpy as np
import re
from imutils.perspective import four_point_transform 
import imutils
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
# Instantiates a client
client = vision.ImageAnnotatorClient()
def detect_text(path):
global lotid
"""Detects text in the file."""
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.text_detection(image=image)
texts = response.text_annotations
string = ''
for text in texts:
string+=' ' + text.description
string = string[0:9]
return string
cap = cv2.VideoCapture(0)
while(True):  
# cap.isOpened()
# Capture frame-by-frame
ret, frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 50, 200, 255)
# find contours in the edge map, then sort them by their size in descending order
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
displayCnt = None
for c in cnts:
peri = cv2.arcLength(c,True)
approx = cv2.approxPolyDP(c,.02 * peri, True)
if len(approx) == 4:
displayCnt = approx
break
# extract the display, apply a perspective transform to it
warped = four_point_transform(gray, displayCnt.reshape(4, 2))
output = four_point_transform(frame, displayCnt.reshape(4, 2))
(h,w) = warped.shape
(dX,dY) = (int(w*.8),int(h*.45))
crop = warped[20:dY,w-dX:w-20]

file = 'live.png'
cv2.imwrite(file,crop)
# print OCR text
print(detect_text(file))
# Display the resulting frame
cv2.imshow('frame',crop)
k = cv2.waitKey(30) &0xff
if k == 27:
break
# print('Confidence: {}'.format(detect_text.confidence))
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

你可以用try,catch来做?它非常简单,并且可以与Python一起使用,请查看尝试除了在Python中

谢谢@PetarMarkovic我围绕扭曲的输出变量和打印语句重新定位了我的 try 异常语句,它的工作方式符合预期。

最新更新