python - 实时网络摄像头源上的 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 中正常工作,请查看Try except in Python
推荐阅读
- xcode - Xcode:无法加载 Info.plist 异常 (eGPUOverrides)
- amazon-web-services - AWS Lambda - 存储队列状态
- r - 在 ggplot2 中进行分面时仅保留 som 组
- tensorflow - 如何使用张量板将我的训练和验证准确性合并到一张图中
- apache-spark - 如何在 Spark 中设置“映射器”/分区的数量
- tizen - 三星 Tizen TV 中的浏览器回调
- verilog - 16 位 CPU 设计:实现获取执行周期的问题
- testing - Python 动态测试计划生成
- ios - 添加到滚动视图内容视图的最后一个视图位于主页指示器后面
- java - Java - 如何在 Arangodb 上创建边缘?