Skip to content

Photo using webcam

import cv2
# 0 => Internal Webcam

# Connect to webcam
cap = cv2.VideoCapture(0)
# To take photo
status, photo = cap.read()
# Status: Success (True) or Failure (False)
# photo: Photo
# Display photo
cv2.imshow('Window Name', photo)
cv2.waitKey()
cv2.destroyAllWindows()
# Release Camara
cap.release()
# Sometimes this doesnt work in windows

Crop face from image and add to top left corner of actual image

Modifies original, as we are using reference

# Crop Face => This is a new image
crop = photo[ 200:500, 250:400 ] # This is reference, any change in crop will change original photo
#type(crop)
crop.shape
(280, 150, 3)
# Display Photo
cv2.imshow('New Window Name', crop)
cv2.waitKey()
cv2.destroyAllWindows()
# Resize
crop_resize = cv2.resize(crop, (100, 100))
# Add this cropped image to the initial image
photo[0:100, 0:100] = crop_resize
# Print merged photo
cv2.imshow('New Window Name 2', photo)
cv2.waitKey()
cv2.destroyAllWindows()

Create copy and then display changes

# Take new photo
cap_2 = cv2.VideoCapture(0)
# To take photo
status, photo_2 = cap_2.read()
# Status: Success (True) or Failure (False)
# photo: Photo
# Display photo -> If we wont use waitKey() and destroyAllWindows(), then jupyter will hang and we would have to restart the kernel

# Display Photo in new Window
cv2.imshow('Window Name', photo_2)

# Keep Window Active until a key is pressed
cv2.waitKey()

# When key is pressed, destroy th window
cv2.destroyAllWindows()
# Release Camara
cap_2.release()
# Sometimes this doesnt work in windows
new_photo = photo_2.copy()
crop_2 = new_photo[ 200:500, 250:400 ]
crop_2.shape
(280, 150, 3)
# Resize
crop_resize_2 = cv2.resize(crop, (100, 100))
# Add this cropped image to the initial image
photo_2[0:100, 0:100] = crop_resize
# Print merged photo
cv2.imshow('Window Name', photo)
cv2.waitKey()
cv2.destroyAllWindows()
Back to top