如何使用网络摄像头捕获图像并使用python提取图像上的信息?

问题描述 投票:0回答:1

[我试图在网络摄像头上捕获图像,并使用python语言提取文本信息。

这里是代码:

import cv2
import matplotlib.pyplot as plt
import numpy as np
import pytesseract

from PIL import Image
from pytesseract import image_to_string

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

def main():
    # Use the attached camera to capture images
    # 0 stands for the first one
    cap = cv2.VideoCapture(0)   

    if cap.isOpened():
        ret, frame = cap.read()
        print(ret)
        print(frame)
    else:
        ret = False

    img1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    # img = Image.open('image.jpg')
    text = pytesseract.image_to_string(img1)
    print(text)

    # plt.imshow(img1)
    # plt.title('Color Image RGB')
    # plt.xticks([])
    # plt.yticks([])
    # plt.show()


    cap.release()

if __name__ == "__main__":
main()

该代码无效。我在Youtube上观看了一些视频,并且看到人们通常使用Image.open(“ image.jpg”)打开计算机上的图像。但是我需要从网络摄像头捕获图像并提取其信息。因此,这种方法在我的情况下不起作用。有没有办法将这两种方法结合起来?像使用cv2捕获图像并使用pytesseract.image_to_string()提取信息一样?

python cv2 python-tesseract
1个回答
0
投票

您能代替下面的代码行来尝试吗,

text = pytesseract.image_to_string(img1)

使用代码,

text = pytesseract.image_to_string(Image.fromarray(img1))

或者在这里有一个有效的代码段,(复制您的代码并稍作更新),

def main():
    # Use the attached camera to capture images
    # 0 stands for the first one
    cap = cv2.VideoCapture(0)
    while cap.isOpened():
        ret, frame = cap.read()
        img1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        text = pytesseract.image_to_string(Image.fromarray(img1))
        cv2.imshow('frame', img1)
        if cv2.waitKey(0) & 0xFF == ord('q'):
            return None
        print("Extracted Text: ", text)
    cap.release()

希望这会为您提供帮助。

我用了看,因为如果没有条件,我没有得到结果,试图弄清楚。

© www.soinside.com 2019 - 2024. All rights reserved.