Hello,
I am new with the Jetson Nano board and I have some troubles with my CSI camera through python
The goal to this program is to classify the frames from the CSI camera of my Jetson Nano with a deep learning model.
I trained it with pictures I took with the CSI camera with the command : nvgstcapture-1.0 --image-res=3 in the console.
The program is working well when I use a USB webcam with the command : cap = cv2.VideoCapture(1)
I can get my visualize_model function for each frames so everything is fine.
But I d like to use the CSI camera because I trained my model with.
When I try to use the CSI camera with GSTREAMER, the window with the frame opens (there is a big latency, like 2 or 3 sec) and either the window crashes or either I can press Escape key to close the windows but the program is not closing (I have to stop it with keyboard interrupt) and it doesn t print my visualize_model function…
Is someone knows how I can fix my problem ?
Here the program :
> import cv2
> import torch
> import torch.nn as nn
> import torch.optim as optim
> from torch.optim import lr_scheduler
> import numpy as np
> import torchvision
> from torchvision import datasets, models, transforms
> import os
> import time
> import copy
> import torch.nn.functional as f
> from PIL import Image
>
>
> def gstreamer_pipeline(
> capture_width=1280,
> capture_height=720,
> display_width=1280,
> display_height=720,
> framerate=60,
> flip_method=0,
> ):
> return (
> "nvarguscamerasrc ! "
> "video/x-raw(memory:NVMM), "
> "width=(int)%d, height=(int)%d, "
> "format=(string)NV12, framerate=(fraction)%d/1 ! "
> "nvvidconv flip-method=%d ! "
> "video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
> "videoconvert ! "
> "video/x-raw, format=(string)BGR ! appsink"
> % (
> capture_width,
> capture_height,
> framerate,
> flip_method,
> display_width,
> display_height,
> )
> )
>
> data_dir = '/home/Name/Classification'
> model_dir = data_dir+"/best_model_mobilenet_v2.pth"
>
> device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
> model = torch.load(model_dir)
> model = model.to(device)
> was_training = model.training
> model.eval()
>
> def visualize_model(model, image=''):
>
> imsizel=700
> imsizeL=round((imsizel/16)*9)
>
> data_transforms =transforms.Compose([transforms.Resize((imsizeL,imsizel)),transforms.ToTensor(),transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
>
> img = Image.open(image)
> image_datasets = data_transforms(img)
> dataloaders = torch.utils.data.DataLoader(image_datasets, batch_size=1, shuffle=False, num_workers=0)
> class_names = ["B", "G"]
>
> with torch.no_grad():
> inputs = image_datasets
> inputs = inputs.to(device)
> outputs = model(inputs[None])
>
> _, preds = torch.max(outputs, 1)
> precision = f.softmax(outputs, dim=1)
> print('-'*20)
> for k in range(0, len(class_names)):
> print("Probability ", class_names[k] + " : ", round(precision[0][k].item(), 3))
> print('-'*20 + '\n')
> return
>
> cap = cv2.VideoCapture(1)
> #cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
>
> if cap.isOpened():
> window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)
> while cv2.getWindowProperty("CSI Camera", 0) >= 0:
> ret_val, img = cap.read()
> cv2.imwrite('/home/Name/Classification/CamCap/cap.jpg', img)
> image = '/home/Name/Classification/CamCap/cap.jpg'
> visualize_model(model=model, image = image)
> cv2.imshow("CSI Camera", img)
> keyCode = cv2.waitKey(30) & 0xFF
>
> if keyCode == 27:
> break
>
> cap.release()
> cv2.destroyAllWindows()
```Preformatted text