如何在python中使用笔记本电脑的相机捕获照片?

时间:2019-05-29 02:09:29

标签: python opencv

如何使用python(而非网络摄像头)的便携式相机拍摄照片?实际上,我想从笔记本电脑的前置摄像头捕获照片,但是当我尝试仅前置摄像头的指示灯闪烁时,摄像头没有打开,我正在使用OpenCV和python。

我尝试过的这段代码

for( i in 1:nrow(mtcars)){
    mtcars$my_hp[i] <- ifelse(mtcars$hp[i] < 50, "Small",
                                                            ifelse(mtcars$hp[i]< 100, "Medium",
                                                                         "Large"))

}

我想打开笔记本电脑的相机并使用OpenCV和python捕获图片

4 个答案:

答案 0 :(得分:0)

也许,您应该阅读官方文档。您尝试使用此代码。祝你好运!

mov ecx, [ebp+8]

更多信息click here

答案 1 :(得分:0)

首先尝试像管理员一样运行python的shell /控制台以执行脚本: 该代码应该可以正常工作:

import cv2 #to take photos or pre-process it with some computer vision technique
import os #to save images at any path i.g E:/myfolder/images/
import argparse #to receive parameters in the console
#Open the camera:
cam = cv2.VideoCapture(0)


ap = argparse.ArgumentParser()
ap.add_argument("-c", "--quantity", required=True, help="Set the quantity of images that you want to take p. ej. 350")
args = vars(ap.parse_args())

#set path where we are going to save the image.
outDirectory = "E:/alxor/"

def takePhoto(number):
    if cam.isOpened():
        print("Camera opened successfully!")
        #Get one image:
        ret, frame = cam.read()
        name = "image_"+str(number)+".jpg"
        print(name)
        cv2.imwrite(os.path.join(outDirectory, name), frame)
    else:
        print("[INFO] Can not open the camera :(")
c = int(args["quantity"])
j = 1
while j <= c: #introduciendo 's' salimos del bucle

    print ("[INFO] WRITE 's' TO EXIT: ")
    print ("[INFO] WRITE 'c' TO TAKE A PHOTO ")
    user_input = input() #leer entrada
    if user_input is 'c':

        takePhoto(j)
        print("[INFO] IMAGE #"+str(j)+" SAVED...")

    if user_input is 's':
        break
        print("[INFO] THE PROGRAM HAS FINISHED..")
    j+=1

#Turn off the camera..
cam.release()
print("[INFO] THE PROGRAM HAS FINISHED..")

更新1.使用os库将图像保存在所需路径中。 更新2.拍摄n张图像的选项 我也将此代码留在github中: code

答案 2 :(得分:0)

这里是从网络摄像头捕获图像的示例。这是较新的面向对象的,整合的OpenCV 2 Python API。

import cv2

# Camera 0 is your port number 
camera_port = 0

#Number of frames to throw away while the camera adjusts to light levels
ramp_frames = 30

# Initialize cam with port
camera = cv2.VideoCapture(camera_port)

# Captures a single image & returns  in PIL format
def get_image():

# read full image out of a VideoCapture object.
retval, im = camera.read()
return im

# Ramp the camera - these frames will be discarded and are only used to allow v4l2
for i in xrange(ramp_frames):
temp = get_image()
print("Taking image...")

# Take the actual image we want to keep
camera_capture = get_image()
file = "/home/codeplasma/test_image.png"

# correct format based on the file extension you provide. Convenient!
cv2.imwrite(file, camera_capture)

# capture object until your script exits
del(camera)

答案 3 :(得分:0)

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
     # Capture frame-by-frame
     ret, frame = cap.read()

# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Display the resulting frame
cv2.imshow('frame',gray)
#this line save your image into Dir
cv2.imwrite("you dir path eg. C:\", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

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