ishaan arora
ishaan arora

Reputation: 541

Capturing Image from web cam in Python

import cv2.cv as cv 
import numpy as np

Capture = cv.CaptureFromCAM(0)
image = cv.QueryFrame(Capture) #here you have an IplImage
imgarray = np.asarray(image[:,:]) #this is the way I use to convert it to numpy ar

I am using this code to capture image from webcam and i want to show it on canvas,please help.the above code just activates the web cam without any error ,please can you either help may save the IplImage on hard-disk or display it on canvas.Really urgent.This is a part of the project i am doing. Help will be appreciated

I used this code using pygame

import pygame
import pygame.camera
from pygame.locals import *
import pygame.image

pygame.init()
pygame.camera.init()

camlist = pygame.camera.list_cameras()
cam = pygame.camera.Camera(camlist[0],(640,480))

if camlist:
    cam = pygame.camera.Camera(camlist[0],(640,480))
cam.start()
image = cam.get_image()

I used this code to capture image from web cam and do the same but,it does not do anything please help me either store the image on hard-disk or display it on canvas

Upvotes: 0

Views: 2657

Answers (1)

Vipul
Vipul

Reputation: 4168

import cv2
cam = cv2.VideoCapture(0)
winName = "image"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
s, im = cam.read() # captures image
cv2.imshow(winName, im) # displays captured image
cv2.imwrite("test.bmp",im) # writes image test.bmp to disk
cv2.waitKey()

Upvotes: 2

Related Questions