Skip to content

Instantly share code, notes, and snippets.

@jgaspar10
Last active January 27, 2023 17:02
Show Gist options
  • Save jgaspar10/9872274bcef66135601c745e421514eb to your computer and use it in GitHub Desktop.
Save jgaspar10/9872274bcef66135601c745e421514eb to your computer and use it in GitHub Desktop.
So guys I have provided this snippet of code to help anyone having issues resizing their webcam window size on Python Jupyter Notebook
# The is a snippet of code taken from my Python Sign Language Detection repository.
# This specific snippet is using mediapipe holistic along with the webcamera which is defined as "cap = cv2.VideoCapture(0)"
# This snippet will start your webcam but it will so in a very small window (640,480)
cap = cv2.VideoCapture(0)
## This will set the mediapipe model
with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
while cap.isOpened():
## To read feed
ret, frame = cap.read()
## To make detections
image, results = mediapipe_detection(frame, holistic)
print(results)
## To draw landmarks
draw_styled_landmarks(image, results)
## To show to screen
cv2.imshow('OpenCV Feed', image)
## To quit camera window
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
------------To rezise the window to a custom size that you want, you can do this by adding the following code:-----------
def make_1080p():
cap.set(3, 1920)
cap.set(4, 1080)
def make_720p():
cap.set(3, 1280)
cap.set(4, 720)
def make_480p():
cap.set(3, 640)
cap.set(4, 480)
def change_res(width, height):
cap.set(3, width)
cap.set(4, height)
make_720p()
change_res(1280, 720)
----------------------------- Add this at the beggining of the code like this:-------------------------
cap = cv2.VideoCapture(0)
def make_1080p():
cap.set(3, 1920)
cap.set(4, 1080)
def make_720p():
cap.set(3, 1280)
cap.set(4, 720)
def make_480p():
cap.set(3, 640)
cap.set(4, 480)
def change_res(width, height):
cap.set(3, width)
cap.set(4, height)
make_720p()
change_res(1280, 720)
# This will set the mediapipe model
with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
while cap.isOpened():
## To read feed
ret, frame = cap.read()
## To make detections
image, results = mediapipe_detection(frame, holistic)
print(results)
## To draw landmarks
draw_styled_landmarks(image, results)
## To show to screen
cv2.imshow('OpenCV Feed', image)
## To quit camera window
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# You are creating 3 different functions each which contain different resolutions for the web window.
# Keep in mind that I have set my "cv2.VideoCapture(0)" to "cap" hence the use of "cap.set"
# If you have set it to a different name make sure you use it, eg: if you have set it to "cam = cv2.VideoCapture(0)"
# Make sure when creating the function, you use "cam.set"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment