def main(): # Get batch size from args batchSize = int(sys.argv[1]) # Set up USB camera (assuming device 0) cam = USBCamera(width=1920, height=1080, capture_device=0) # Set up model for yolov5s yolo = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) device = torch.device('cuda') yolo.to(device) # Basic analytics imageCount = 0 modelTime = 0 # Continue until interrupt try: while True: # Grab as many frames as batch size imgs = [] for i in range(batchSize): imgs.append(cam.read()[:, :, ::-1]) # Convert BGR to RGB # Process with yolo t = time() results = yolo(imgs) modelTime += time() - t # Save files, janky method to change filenames and avoid overwrite for i, f in enumerate(results.files): newNumber = sum(map(int, filter(str.isdigit, f)), imageCount) newStr = str(newNumber).zfill(4) + '.jpg' results.files[i] = newStr results.save() imageCount += batchSize except KeyboardInterrupt: print( f'Ending. Processed {imageCount} images in {modelTime}s, average FPS of {imageCount/modelTime}' )
from jetcam.usb_camera import USBCamera TASK = 'thumbs' CATEGORIES = ['thumbs_up', 'thumbs_down'] DATASETS = 'A' TRANSFORMS = transforms.Compose([ transforms.ColorJitter(0.2, 0.2, 0.2, 0.2), transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) dataset = ImageClassificationDataset(TASK + '_' + DATASETS, CATEGORIES, TRANSFORMS) device = torch.device('cuda') model = torchvision.models.resnet18(pretrained=True) model.fc = torch.nn.Linear(512, len(dataset.categories)) model = model.to(device) model.load_state_dict(torch.load('./my_model.pth')) model = model.eval() camera = USBCamera(capture_device=0) while True: image = camera.read() preprocessed = preprocess(image) output = model(preprocessed) output = F.softmax(output, dim=1).detach().cpu().numpy().flatten() print('%5.2f %5.2f' % (output[0], output[1]))
#vs = CSICamera(width=224, height=224, capture_width=1080, capture_height=720, capture_fps=30) vs = USBCamera(capture_device=0) writer = None (W, H) = (None, None) # Start time start = time.time() # loop over frames from the video file stream count = 0 # whileTrue: for count in range(0,700): # read the next frame from the file if use_video_as_input: (grabbed, frame) = vs.read() else: frame = vs.read() # if the frame was not grabbed, then we have reached the end # of the stream #if not grabbed: # break # if the frame dimensions are empty, grab them if W is None or H is None: (H, W) = frame.shape[:2] # clone the output frame, then convert it from BGR to RGB # ordering, resize the frame to a fixed 224x224, and then # perform mean subtraction
# Default resolutions of the frame are obtained.The default resolutions are system dependent. # We convert the resolutions from float to integer. w = 1280 #int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH )) h = 720 #int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT )) fps = 60 # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file. #fourcc = cv2.VideoWriter_fourcc(*'XVID') # 9FPS #fourcc = cv2.VideoWriter_fourcc(*'X264') # 6FPS fourcc = cv2.VideoWriter_fourcc(*'MJPG') # 20FPS video_writer = cv2.VideoWriter("output.avi", fourcc, 30, (w, h)) #video_writer = cv2.VideoWriter("/media/ramdisk/output.avi", fourcc, 30, (w, h)) # record video lFPSbeg = datetime.now() while True: try: frame = capture.read() video_writer.write(frame) # #fps computation cts = datetime.now() lFPSrun = (cts - lFPSbeg).seconds * 1000 + ( cts - lFPSbeg).microseconds / 1000 lFPSfnm = lFPSfnm + 1 if lFPSrun > 0: FPSavg = lFPSfnm * 1000 / lFPSrun else: FPSavg = 0 cfpst = "FPS: %d t: %dsec %.2ffps" % (lFPSfnm, int( lFPSrun / 1000), FPSavg) if use_display: if True or (lFPSfnm % 10) == 0:
# Default resolutions of the frame are obtained.The default resolutions are system dependent. # We convert the resolutions from float to integer. w = 1280 #int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH )) h = 720 #int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT )) fps = 60 # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file. #fourcc = cv2.VideoWriter_fourcc(*'XVID') # 9FPS #fourcc = cv2.VideoWriter_fourcc(*'X264') # 6FPS fourcc = cv2.VideoWriter_fourcc(*'MJPG') # 20FPS video_writer = cv2.VideoWriter("/mnt/Temp/output.avi", fourcc, 30, (w, h)) #video_writer = cv2.VideoWriter("/media/ramdisk/output.avi", fourcc, 30, (w, h)) # record video lFPSbeg = datetime.now() while True: try: frame = camera.read() #frame, width, height = camera.CaptureRGBA (zeroCopy=1) #frame = jetson.utils.cudaToNumpy (frame, width, height, 4) # type: np.ndarray video_writer.write(frame) # #fps computation cts = datetime.now() lFPSrun = (cts - lFPSbeg).seconds * 1000 + ( cts - lFPSbeg).microseconds / 1000 lFPSfnm = lFPSfnm + 1 if lFPSrun > 0: FPSavg = lFPSfnm * 1000 / lFPSrun else: FPSavg = 0 cfpst = "FPS: %d t: %dsec %.2ffps" % (lFPSfnm, int( lFPSrun / 1000), FPSavg)
def main(): liveDemo = True if liveDemo: # Set up USB camera (assuming device 0) cam = USBCamera(width=1920, height=1080, capture_device=0) # Set up model for yolov5s yolo = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) device = torch.device('cuda') yolo.to(device) # Set up topology, model, and classes for ResNet with open('human_pose.json', 'r') as f: human_pose = json.load(f) topology = trt_pose.coco.coco_category_to_topology(human_pose) resnet = TRTModule() resnet.load_state_dict(torch.load('resnet_trt.pth')) parseObjects = ParseObjects(topology) drawObjects = DrawObjects(topology) # Basic analytics imageCount = 0 t = time() # Live demo on webcam if liveDemo: # Continue until interrupt try: while True: # Grab a frame img = cam.read()[:, :, ::-1] # Convert BGR to RGB print(f'got frame {imageCount}') # Process with yolo and resnet result, empty = processFrame(img, yolo, resnet, parseObjects, drawObjects) # Save file cv2.imwrite(f'imgs/{imageCount:04}.jpg', result) imageCount += 1 except KeyboardInterrupt: print('Keyboard interrupt!') finally: t = time() - t # Recorded video else: cap = cv2.VideoCapture('example_video.mpg') # Grab a frame ret, frame = cap.read() # Continue until video is done while ret: # Process and save image img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) result, empty = processFrame(img, yolo, resnet, parseObjects, drawObjects) cv2.imwrite(f'imgs/{imageCount:04}.jpg', result) # Try to grab next frame ret, frame = cap.read() imageCount += 1 t = time() - t cap.release() print( f'Ending. Processed {imageCount} images in {t}s, average FPS of {imageCount/t}' )
cv2.setWindowProperty("BubbleBop", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) camera = USBCamera(capture_device=CAPTURE_DEVICE, width=IMAGE_SHAPE[0], height=IMAGE_SHAPE[1]) pose_detector = PoseDetector(POSE_MODEL, POSE_INPUT_SHAPE) left_sampler = CircleSampler(LEFT_SAMPLE_AREA_CENTER, SAMPLE_AREA_RADIUS) right_sampler = CircleSampler(RIGHT_SAMPLE_AREA_CENTER, SAMPLE_AREA_RADIUS) left_bubble = to_int(left_sampler.sample()) right_bubble = to_int(right_sampler.sample()) left_wrist = (0, 0) right_wrist = (0, 0) bubble_radius = MIN_BUBBLE_RADIUS bubble_speed = MIN_BUBBLE_SPEED score = 0 image = np.copy(camera.read()[:, ::-1]) # RUN while True: # DISPLAY FINAL SCORE IF FINISHED while bubble_radius > MAX_BUBBLE_RADIUS: # image = cv2.putText(image, 'SCORE: %d (FINAL)' % score, TEXT_ORIGIN, TEXT_FONT, # TEXT_FONT_SCALE, TEXT_COLOR, TEXT_THICKNESS, cv2.LINE_AA) cv2.imshow('BubbleBop', image) if cv2.waitKey(1) & 0xFF == ord('q'): score = 0 bubble_radius = MIN_BUBBLE_RADIUS