Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I zoom my webcam in Open CV Python?

I want my webcam to be zoomed in open cv python and I don't know how. Can anyone help me with my problem?

import cv2
video = cv2.VideoCapture(0)
while True:
    check, frame = video.read()
    cv2.imshow('Video', frame)
    key = cv2.waitKey(1)
    if key == 27:
        break
  video.release()
  cv2.destroyAllWindows
like image 523
Hayasiiiint Avatar asked Jun 15 '18 06:06

Hayasiiiint


People also ask

How do I access webcam with cv2?

Using cap = cv2. VideoCapture(0) zero, the camera found is the webcam. If you connect any other camera in the same PC, you need to change the video node 1 instead of 0. Please change node 1 in the "cap = cv2.


1 Answers

You can use this solution. It makes the job -> croping + zoom + array up and array down.

import cv2

        def show_webcam(mirror=False):
            scale=10

            cam = cv2.VideoCapture(0)
            while True:
                ret_val, image = cam.read()
                if mirror: 
                    image = cv2.flip(image, 1)


                #get the webcam size
                height, width, channels = image.shape

                #prepare the crop
                 centerX,centerY=int(height/2),int(width/2)
                radiusX,radiusY= int(scale*height/100),int(scale*width/100)

                minX,maxX=centerX-radiusX,centerX+radiusX
                minY,maxY=centerY-radiusY,centerY+radiusY

                cropped = image[minX:maxX, minY:maxY]
                resized_cropped = cv2.resize(cropped, (width, height)) 

                cv2.imshow('my webcam', resized_cropped)
                if cv2.waitKey(1) == 27: 
                    break  # esc to quit

                #add + or - 5 % to zoom

                if cv2.waitKey(1) == 0: 
                    scale += 5  # +5

                if cv2.waitKey(1) == 1: 
                    scale = 5  # +5

            cv2.destroyAllWindows()


        def main():
            show_webcam(mirror=True)


        if __name__ == '__main__':
            main()
like image 126
Amara BOUDIB Avatar answered Oct 05 '22 00:10

Amara BOUDIB