从网络摄像头流读取实时图像

时间:2020-10-04 11:04:51

标签: image-processing ocr speech-recognition text-to-speech webcam-capture

`我正在使用gTTS进行语音识别项目。问题是,当我运行代码时,系统没有响应。当图片不包含文字时,代码停止工作(无法阅读文字),据我所知,我尝试了但无法解决。如果有人可以帮助我解决此问题,我将不胜感激。首先十分感谢。这是我的代码:

import pytesseract
import numpy as np
from PIL import ImageGrab
import time
import requests
import io
import json
from gtts import gTTS
import os
import pyttsx3


pytesseract.pytesseract.tesseract_cmd  = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'
#img = cv2.imread('screenshotfr.JPG')
#import cv2
# Loading Video
import cv2
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10,150)


while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    file = 'live.png'
    cv2.imwrite(file, frame)

    # print OCR text
    fh = open('text.txt', 'w')
    texti = pytesseract.image_to_string(file)
    print(texti)
    fh.write(texti)
    # Display the resulting frame

    cv2.imshow('frame', frame)
    fh.close()
    fh = open('text.txt', 'r')
    my_text = fh.read().replace("\n", " ")
    output = gTTS(text=my_text, lang='fr', slow=False)
    output.save("output.mp3")
    fh.close()
    os.system("start output.mp3")


    # When everything done, release the capture
cap.release()
cv2.destroyAllWindows()








0 个答案:

没有答案
相关问题