如何使程序输入从相机拍摄的图像?

2024-03-29 11:07:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个python程序,可以从卡车上读取车牌。由该程序处理并过滤字符作为输出的图像。以下是程序中图像的输入:

img = cv2.imread('image.jpg') #variable 'img' gets processed later using ocr

现在,有没有一种方法可以制作网络摄像头,例如:拍摄一张图像,将其存储在某处,然后使用拍摄的图像运行程序?

使用Python 3.7.2


Tags: 图像image程序img字符cv2variablejpg
2条回答

对于相机/视频,我可以推荐这个OpenCV

import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():
    print("Cannot open camera")
    exit()
while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # Our operations on the frame come here
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv.imshow('frame', gray)
    if cv.waitKey(1) == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

cv.VideoCapture(0)捕获视频帧并ret, frame = cap.read()读取每个帧

您可以使用OpenCV的VideoCapture方法捕获单个帧

import cv2

pic = cv2.VideoCapture(0) # video capture source camera (Here webcam of laptop) 
ret,frame = pic.read() # return a single frame in variable `frame`

while(True):
    cv2.imshow('img1',frame) #display the captured image
    if cv2.waitKey(1) & 0xFF == ord('y'): #save on pressing 'y' 
        cv2.imwrite('images/c1.png',frame)
        cv2.destroyAllWindows()
        break

pic.release()

相关问题 更多 >