How to resize the frame?

Hi there,

I have this OpenCV VideoCapture which is linked to a rstp protocol to connect with an IP Camera. The thing is that when I run the code, I see a very big frame on my laptop and cannot see other functionalities as face detection.

Is there any way to reduce the frame size? I tried cv2.resize(frame, (244, 244)) but that didn’t worked.

here’s my code if anyone would have that better to understand

import cv2
import time
from datetime import datetime


# captures one frame each 5 seconds from camera

cap = cv2.VideoCapture('rtsp://admin:First2022@192.168.88.251/')
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# writer = cv2.VideoWriter('basicvideo.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 20, (width, height))
previous = time.time()
delta = 0

while True:
    ret, frame = cap.read()
    # writer.write(frame)
    cv2.imshow('frame', frame)
    current = time.time()
    delta += current - previous
    previous = current

    now = datetime.now()
    dt_string = now.strftime("%d-%m-%Y %H:%M:%S")
    print(f"current_time: {current} and delta: {delta}")
    # Check if 3 (or some other value) seconds passed
    if delta > 5.0:
        # Operations on image
        # Reset the time counter
        delta = 0
        print("Well 5 secodns have passed...")
        cv2.imwrite(filename=f"{dt_string}.png", img=frame)

    if cv2.waitKey(1) & 0xFF == ord('c'):
        cv2.imwrite(filename="test2.png", img=frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
# writer.release()
cv2.destroyAllWindows()

what exactly did happen?

you probably didn’t even check that your VideoCapture gave you an actual picture.

what exactly did happen?

When I ran the script it showed me the same big size that I had, it looked like it had no effect on the code.

do you realize that the function call returns something?

def resize(src, dsize, dst=None, fx=None, fy=None, interpolation=None): # real signature unknown; restored from __doc__
    """
    resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) -> dst
    .   @brief Resizes an image.
    .   
    .   The function resize resizes the image src down to or up to the specified size. Note that the
    .   initial dst type or size are not taken into account. Instead, the size and type are derived from
    .   the `src`,`dsize`,`fx`, and `fy`. If you want to resize src so that it fits the pre-created dst,
    .   you may call the function as follows:
    .   @code
    .       // explicitly specify dsize=dst.size(); fx and fy will be computed from that.
    .       resize(src, dst, dst.size(), 0, 0, interpolation);
    .   @endcode
    .   If you want to decimate the image by factor of 2 in each direction, you can call the function this
    .   way:
    .   @code
    .       // specify fx and fy and let the function compute the destination image size.
    .       resize(src, dst, Size(), 0.5, 0.5, interpolation);
    .   @endcode
    .   To shrink an image, it will generally look best with #INTER_AREA interpolation, whereas to
    .   enlarge an image, it will generally look best with c#INTER_CUBIC (slow) or #INTER_LINEAR
    .   (faster but still looks OK).
    .   
    .   @param src input image.
    .   @param dst output image; it has the size dsize (when it is non-zero) or the size computed from
    .   src.size(), fx, and fy; the type of dst is the same as of src.
    .   @param dsize output image size; if it equals zero, it is computed as:
    .    \f[\texttt{dsize = Size(round(fx*src.cols), round(fy*src.rows))}\f]
    .    Either dsize or both fx and fy must be non-zero.
    .   @param fx scale factor along the horizontal axis; when it equals 0, it is computed as
    .   \f[\texttt{(double)dsize.width/src.cols}\f]
    .   @param fy scale factor along the vertical axis; when it equals 0, it is computed as
    .   \f[\texttt{(double)dsize.height/src.rows}\f]
    .   @param interpolation interpolation method, see #InterpolationFlags
    .   
    .   @sa  warpAffine, warpPerspective, remap
    """
    pass

bruh fr? how can I resize the frame then???

you’re in the wrong file. that’s a stub for dumb IDEs that don’t understand how OpenCV works.

the real code is of course not python, but C++.

work with https://docs.opencv.org/

Don’t really understand that website and which file I should download… how can I resize the frame using python though?

I said the function returns the new picture, so you must do this:

small_frame = cv2.resize(frame, (244, 244))

you don’t “download” anything from the docs website. you click on the first/second link you can find, which leads to https://docs.opencv.org/4.x/