程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to open USB or laptop front camera with OpenCV Python

編輯:Python

Catalog

opencv-python open USB Or laptop front camera

Code

effect

Opencv-python Camera recording screen , Taking pictures

Opencv-python The camera records the video and saves it

Taking pictures

opencv-python open USB Or laptop front camera Code

among video_index It's the camera number , Generally, the front camera is 0,USB A camera for 1 or 2.

import cv2def catch_video(name='my_video', video_index=0): # cv2.namedWindow(name) cap = cv2.VideoCapture(video_index) # Create a camera recognition class if not cap.isOpened(): # If no camera is detected , Report errors raise Exception('Check if the camera is on.') while cap.isOpened(): catch, frame = cap.read() # Read every picture cv2.imshow(name, frame) # stay window Show pictures on key = cv2.waitKey(10) if key & 0xFF == ord('q'): # Press q sign out break if cv2.getWindowProperty(name, cv2.WND_PROP_AUTOSIZE) < 1: # spot x sign out break # Release camera cap.release() cv2.destroyAllWindows()if __name__ == "__main__": catch_video() effect

Opencv-python Camera recording screen , Taking pictures Opencv-python The camera records the video and saves it import cv2 # Import opencv package video = cv2.VideoCapture(0) # Turn on the camera fourcc = cv2.VideoWriter_fourcc(*'XVID') # Format of video storage fps = video.get(cv2.CAP_PROP_FPS) # Frame rate # The width and height of the video size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)))out = cv2.VideoWriter('video.avi', fourcc, fps, size) # Video storage f=0while out.isOpened(): ret, img = video.read() # Start using the camera to read data , return ret by true,img Image read for if ret is False: # ret by false Then close exit() cv2.namedWindow('video', cv2.WINDOW_AUTOSIZE) # Create a file called video The window of cv2.imshow('video', img) # Capture the captured image in video Window display out.write(img) # Store the captured image print(f ,' ' , 'fps: ',fps) f+=1 # Press esc Key exit program if cv2.waitKey(1) & 0xFF == 27: video.release() # Turn off camera break Taking pictures #coding:utf-8import cv2cap = cv2.VideoCapture(0)# Create a VideoCapture object flag = 1 # Set a flag , Used to output video information num = 1 # Increasing , Used to save the filename while(cap.isOpened()):# Cycle through every frame ret_flag, Vshow = cap.read() # Return two parameters , The first is bool Is it open normally , The second is the photo array , If only one is set, it becomes a tumple contain bool And pictures cv2.imshow("Capture_Test",Vshow) # Window display , The display name is Capture_Test k = cv2.waitKey(1) & 0xFF # Data delay per frame 1ms, The delay cannot be 0, Otherwise, the read result will be a static frame if k == ord('s'): # If a key is detected ‘s', Print string cv2.imwrite("/home/jie/ picture /VOC/image/"+ str(num) + ".jpg", Vshow) print(cap.get(3)); # Get the length and width print(cap.get(4)); print("success to save"+str(num)+".jpg") print("-------------------------") num += 1 elif k == ord('q'): # If a key is detected ‘q', sign out breakcap.release() # Release camera

The above is personal experience , I hope I can give you a reference , I also hope you can support the software development network .



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved