Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
0
I am streaming lepton FLIR camera on jetson nano using python3 and OpenCV and I have problems that I can not resize live video. The original lepton resolution is (160 x 120) and I would like to resize it to (640 x 480). I tried to use different OpenCV commands but it does not work

need help

Herein the code

<pre># imports<br />
import cv2<br />
import numpy as np<br />
import time<br />
import argparse<br />
<br />
# own modules<br />
import utills, plot<br />
<br />
confid = 0.5<br />
thresh = 0.5<br />
mouse_pts = []<br />
<br />
<br />
# Function to get points for Region of Interest(ROI) and distance scale. It will take 8 points on first frame using mouse click    <br />
# event.First four points will define ROI where we want to moniter social distancing. Also these points should form parallel  <br />
# lines in real world if seen from above(birds eye view). Next 3 points will define 6 feet(unit length) distance in     <br />
# horizontal and vertical direction and those should form parallel lines with ROI. Unit length we can take based on choice.<br />
# Points should pe in pre-defined order - bottom-left, bottom-right, top-right, top-left, point 5 and 6 should form     <br />
# horizontal line and point 5 and 7 should form verticle line. Horizontal and vertical scale will be different. <br />
<br />
# Function will be called on mouse events                                                          <br />
<br />
def get_mouse_points(event, x, y, flags, param):<br />
<br />
    global mouse_pts<br />
    if event == cv2.EVENT_LBUTTONDOWN:<br />
        if len(mouse_pts) < 4:<br />
            cv2.circle(image, (x, y), 5, (0, 0, 255), 10)<br />
        else:<br />
            cv2.circle(image, (x, y), 5, (255, 0, 0), 10)<br />
            <br />
        if len(mouse_pts) >= 1 and len(mouse_pts) <= 3:<br />
            cv2.line(image, (x, y), (mouse_pts[len(mouse_pts)-1][0], mouse_pts[len(mouse_pts)-1][1]), (70, 70, 70), 2)<br />
            if len(mouse_pts) == 3:<br />
                cv2.line(image, (x, y), (mouse_pts[0][0], mouse_pts[0][1]), (70, 70, 70), 2)<br />
        <br />
        if "mouse_pts" not in globals():<br />
            mouse_pts = []<br />
        mouse_pts.append((x, y))<br />
        #print("Point detected")<br />
        #print(mouse_pts)<br />
        <br />
<br />
<br />
def calculate_social_distancing(vid_path, net, output_dir, output_vid, ln1):<br />
    <br />
    count = 0<br />
    vs = cv2.VideoCapture(0)<br />
<br />
    # Get video height, width and fps<br />
    height = int(vs.get(cv2.CAP_PROP_FRAME_HEIGHT))<br />
    width = int(vs.get(cv2.CAP_PROP_FRAME_WIDTH))<br />
    fps = int(vs.get(cv2.CAP_PROP_FPS))<br />
    <br />
    # Set scale for birds eye view<br />
    # Bird's eye view will only show ROI<br />
    scale_w, scale_h = utills.get_scale(width, height)<br />
<br />
    fourcc = cv2.VideoWriter_fourcc(*"XVID")<br />
    output_movie = cv2.VideoWriter("./output_vid/distancing.avi", fourcc, fps, (width, height))<br />
    bird_movie = cv2.VideoWriter("./output_vid/bird_eye_view.avi", fourcc, fps, (int(width * scale_w), int(height * scale_h)))<br />
        <br />
    points = []<br />
    global image<br />
    <br />
    while True:<br />
<br />
        (grabbed, frame) = vs.read()<br />
<br />
        if not grabbed:<br />
            print('here')<br />
            break<br />
            <br />
        (H, W) = frame.shape[:2]<br />
        <br />
        # first frame will be used to draw ROI and horizontal and vertical 180 cm distance(unit length in both directions)<br />
        if count == 0:<br />
            while True:<br />
                image = frame<br />
                cv2.imshow("image", image)<br />
                cv2.waitKey(1)<br />
                if len(mouse_pts) == 8:<br />
                    cv2.destroyWindow("image")<br />
                    break<br />
               <br />
            points = mouse_pts      <br />
                 <br />
        # Using first 4 points or coordinates for perspective transformation. The region marked by these 4 points are <br />
        # considered ROI. This polygon shaped ROI is then warped into a rectangle which becomes the bird eye view. <br />
        # This bird eye view then has the property property that points are distributed uniformally horizontally and <br />
        # vertically(scale for horizontal and vertical direction will be different). So for bird eye view points are <br />
        # equally distributed, which was not case for normal view.<br />
        src = np.float32(np.array(points[:4]))<br />
        dst = np.float32([[0, H], [W, H], [W, 0], [0, 0]])<br />
        prespective_transform = cv2.getPerspectiveTransform(src, dst)<br />
<br />
        # using next 3 points for horizontal and vertical unit length(in this case 180 cm)<br />
        pts = np.float32(np.array([points[4:7]]))<br />
        warped_pt = cv2.perspectiveTransform(pts, prespective_transform)[0]<br />
        <br />
        # since bird eye view has property that all points are equidistant in horizontal and vertical direction.<br />
        # distance_w and distance_h will give us 180 cm distance in both horizontal and vertical directions<br />
        # (how many pixels will be there in 180cm length in horizontal and vertical direction of birds eye view),<br />
        # which we can use to calculate distance between two humans in transformed view or bird eye view<br />
        distance_w = np.sqrt((warped_pt[0][0] - warped_pt[1][0]) ** 2 + (warped_pt[0][1] - warped_pt[1][1]) ** 2)<br />
        distance_h = np.sqrt((warped_pt[0][0] - warped_pt[2][0]) ** 2 + (warped_pt[0][1] - warped_pt[2][1]) ** 2)<br />
        pnts = np.array(points[:4], np.int32)<br />
        cv2.polylines(frame, [pnts], True, (70, 70, 70), thickness=2)<br />
    <br />
    ####################################################################################<br />
    <br />
        # YOLO v4<br />
        blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (128, 128), swapRB=True, crop=False)<br />
        net.setInput(blob)<br />
        start = time.time()<br />
        layerOutputs = net.forward(ln1)<br />
        end = time.time()<br />
        boxes = []<br />
        confidences = []<br />
        classIDs = []   <br />
    <br />
        for output in layerOutputs:<br />
            for detection in output:<br />
                scores = detection[5:]<br />
                classID = np.argmax(scores)<br />
                confidence = scores[classID]<br />
                # detecting humans in frame<br />
                if classID == 1:<br />
<br />
                    if confidence > confid:<br />
<br />
                        box = detection[0:4] * np.array([W, H, W, H])<br />
                        (centerX, centerY, width, height) = box.astype("int")<br />
<br />
                        x = int(centerX - (width / 2))<br />
                        y = int(centerY - (height / 2))<br />
<br />
                        boxes.append([x, y, int(width), int(height)])<br />
                        confidences.append(float(confidence))<br />
                        classIDs.append(classID)<br />
                    <br />
        idxs = cv2.dnn.NMSBoxes(boxes, confidences, confid, thresh)<br />
        font = cv2.FONT_HERSHEY_PLAIN<br />
        boxes1 = []<br />
        for i in range(len(boxes)):<br />
            if i in idxs:<br />
                boxes1.append(boxes[i])<br />
                x,y,w,h = boxes[i]<br />
                <br />
        if len(boxes1) == 0:<br />
            count = count + 1<br />
            continue<br />
            <br />
        # Here we will be using bottom center point of bounding box for all boxes and will transform all those<br />
        # bottom center points to bird eye view<br />
        person_points = utills.get_transformed_points(boxes1, prespective_transform)<br />
        <br />
        # Here we will calculate distance between transformed points(humans)<br />
        distances_mat, bxs_mat = utills.get_distances(boxes1, person_points, distance_w, distance_h)<br />
        risk_count = utills.get_count(distances_mat)<br />
    <br />
        frame1 = np.copy(frame)<br />
        <br />
        # Draw bird eye view and frame with bouding boxes around humans according to risk factor    <br />
        bird_image = plot.bird_eye_view(frame, distances_mat, person_points, scale_w, scale_h, risk_count)<br />
        img = plot.social_distancing_view(frame1, bxs_mat, boxes1, risk_count)<br />
        <br />
        # Show/write image and videos<br />
        if count != 0:<br />
            output_movie.write(img)<br />
            bird_movie.write(bird_image)<br />
    <br />
            cv2.imshow('Bird Eye View', bird_image)<br />
            cv2.imshow('Social Distancing', img)<br />
            cv2.imwrite(output_dir+"frame%d.jpg" % count, img)<br />
            cv2.imwrite(output_dir+"bird_eye_view/frame%d.jpg" % count, bird_image)<br />
    <br />
        count = count + 1<br />
        if cv2.waitKey(1) & 0xFF == ord('q'):<br />
            break<br />
     <br />
    vs.release()<br />
    cv2.destroyAllWindows() <br />
        <br />
<br />
if __name__== "__main__":<br />
<br />
    # Receives arguements specified by user<br />
    parser = argparse.ArgumentParser()<br />
    <br />
    parser.add_argument('-v', '--video_path', action='store', dest='video_path', default='./data/example.mp4' ,<br />
                    help='Path for input video')<br />
                    <br />
    parser.add_argument('-o', '--output_dir', action='store', dest='output_dir', default='./output/' ,<br />
                    help='Path for Output images')<br />
    <br />
    parser.add_argument('-O', '--output_vid', action='store', dest='output_vid', default='./output_vid/' ,<br />
                    help='Path for Output videos')<br />
<br />
    parser.add_argument('-m', '--model', action='store', dest='model', default='./models/',<br />
                    help='Path for models directory')<br />
                    <br />
    parser.add_argument('-u', '--uop', action='store', dest='uop', default='NO',<br />
                    help='Use open pose or not (YES/NO)')<br />
                    <br />
    values = parser.parse_args()<br />
    <br />
    model_path = values.model<br />
    if model_path[len(model_path) - 1] != '/':<br />
        model_path = model_path + '/'<br />
        <br />
    output_dir = values.output_dir<br />
    if output_dir[len(output_dir) - 1] != '/':<br />
        output_dir = output_dir + '/'<br />
    <br />
    output_vid = values.output_vid<br />
    if output_vid[len(output_vid) - 1] != '/':<br />
        output_vid = output_vid + '/'<br />
<br />
<br />
    # load Yolov4 weights<br />
    <br />
    weightsPath = model_path + "yolov4-tiny-custom_best.weights"<br />
    configPath = model_path + "yolov4-tiny-custom.cfg"<br />
<br />
    net_yl = cv2.dnn.readNetFromDarknet(configPath, weightsPath)<br />
    ln = net_yl.getLayerNames()<br />
    ln1 = [ln[i[0] - 1] for i in net_yl.getUnconnectedOutLayers()]<br />
<br />
    # set mouse callback <br />
<br />
    cv2.namedWindow("image")<br />
    cv2.setMouseCallback("image", get_mouse_points)<br />
    np.random.seed(42)<br />
    <br />
    calculate_social_distancing(values.video_path, net_yl, output_dir, output_vid, ln1)</pre>


What I have tried:

I want to resize the streaming video from LEPTON FLIR camera to (640 x 480)
Posted
Comments
Richard MacCutchan 3-Dec-21 4:45am    
"I tried to use different OpenCV commands but it does not work"
No one here can guess what commands you have used, or what you mean by "it does not work". Please use the Improve question link above, and add complete details of what is not working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900