I have a project where I am displaying the live video feed from the USB camera using opencv
. In between the code, there is a condition. When it becomes True
I have to call an API to get some data which I am displaying on the live video feed frame. Calling the API and getting the response takes some time (2-3sec) during which the frame is freezed which doesn't look good. Below is the code snippet:
if config['ROIX1'] < f_startX and config['ROIX2']:
# Call the API and get the response
"""
OTHER CODE
"""
conn = http.client.HTTPSConnection('api.tive.com')
conn.request("POST", "/get_status", json_data, headers)
response = conn.getresponse()
rdata = response.read()
rdata = rdata.decode('utf8')
rdata = json.loads(rdata)
subject = rdata['subject']
txt = "SUBJECT: {}".format(subject)
cv2.putText(frame, txt, (5, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
cv2.imshow(win_name, frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
In above code when the if condition becomes True, code makes a call to the API and the response rdata
takes some time due to which the application freezes for some time. How can I effectively handle calling and getting the response from the API in another thread and once it gets the result, it is shown on frame
. I am not very experienced in threads
. Please help. Thanks