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

Python 如何使程序输入从相机拍摄的图像?,python,python-3.x,opencv,raspberry-pi,Python,Python 3.x,Opencv,Raspberry Pi,我正在开发一个python程序,可以从卡车上读取车牌。由该程序处理并过滤字符作为输出的图像。以下是程序中图像的输入: img = cv2.imread('image.jpg') #variable 'img' gets processed later using ocr 现在,有没有一种方法可以制作网络摄像头,例如:拍摄一张图像,将其存储在某处,然后使用拍摄的图像运行程序? 使用Python 3.7.2可以使用OpenCV方法捕获单个帧 import cv2 pic = cv2.VideoC

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

img = cv2.imread('image.jpg') #variable 'img' gets processed later using ocr
现在,有没有一种方法可以制作网络摄像头,例如:拍摄一张图像,将其存储在某处,然后使用拍摄的图像运行程序?


使用Python 3.7.2

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

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()

对于相机/视频,我可以推荐这一点


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

检查您可以替换相同的img并读取它请不要使用评论空间添加信息;改为编辑并更新您的帖子。
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()