gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:04:04 25 4
gpt4 key购买 nike

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

我试过检查框架是否不是Nonetype,而ret是否为True,但我不知道如何在框架中添加标签之前不输出任何内容。

以下是示例帧的图像以及我的边缘检测的输出
/image/tVHFm.jpg
/image/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()

最佳答案

你可以尝试一下,捕获吗?这很简单,可以在Python上正常工作,请查看Try Except in Python

关于python - 实时网络摄像头订阅源上的OCR:图像高度为零,无效的IHDR数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57500404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com