#!/usr/bin/python # Version adopted for Jetson Nano # import the necessary packages import paho.mqtt.client as mqtt import time import cv2 import threading #VIDEO_URL = "http://f24hls-i.akamaihd.net/hls/live/221147/F24_EN_HI_HLS/master_2000.m3u8" VIDEO_URL = "http://127.0.0.1:1880/mp4frag/france24/video.mp4" class TrtThread(threading.Thread): def __init__(self, client, trt_threadEvent): threading.Thread.__init__(self) self.client = client self.trt_threadEvent = trt_threadEvent def show(self): global th_abort cap = cv2.VideoCapture(VIDEO_URL) fps = 0.0 tic = time.time() while(cap.isOpened() and not th_abort): ret, frame = cap.read() if frame is not None: toc = time.time() curr_fps = 1.0 / (toc - tic) # calculate an exponentially decaying average of fps number fps = curr_fps if fps == 0.0 else (fps*0.95 + curr_fps*0.05) print (fps) tic = toc # cv2.imshow('frame',frame) img_str = cv2.imencode('.jpg', frame)[1].tostring() self.client.publish("result", img_str, 1, False) if cv2.waitKey(1) & 0xFF == ord('q'): break self.trt_threadEvent.wait(0.05) cap.release() del cap th_abort = True cv2.destroyAllWindows() def run(self): print('TrtThread: start running...waiting for stream to start') self.show() class mqttThread(threading.Thread): def __init__(self, client): threading.Thread.__init__(self) self.client = client self.client.on_connect = self.on_connect self.client.on_message = self.on_message self.client.on_subscribe = self.on_subscribe resp = self.client.connect("192.168.0.227", 1883, 60) self.client.loop_start() def on_connect(self, client, userdata, flags, rc): print("Connected to MQTT broker:", rc) client.subscribe("commands", 0) def on_subscribe(self, client, userdata, mid, granted_qos): print ('Subscribed to topics:', userdata, mid, granted_qos) def on_message(self, client, userdata, msg): global th_abort if msg.payload.decode("utf-8") == 'Stop-vtomqtt': th_abort = True def run(self): global th_abort while not th_abort: time.sleep(1) self.client.loop_stop() self.client.disconnect() print ("mqttThread: stopped...") exit() # inits th_abort = False image = None client = mqtt.Mosquitto() mqtt_thread = mqttThread(client,) mqtt_thread.start() # start the child thread trt_threadEvent = threading.Event() trt_thread = TrtThread(client,trt_threadEvent,) trt_thread.start() # start the child thread