I have a camera that takes photos in two different qualities: "cam-lo.jpg":LOW quality that should always be requested to get photos every 0.2 seconds(getting photos fastly gives the effect of a video) and "cam-hi.jpg":HIGH quality. If the distance variable is less or equal to 40, I have to request "http://192.164.1.39/cam-hi.jpg" and stop requesting for the Low-Quality one until I get the high quality picture and do processing with it. For this purpose I try to stop the other thread with python-condition as below:
if distance<=40:
while(True):
**cond.acquire()**
a="stop"
for i in range(5):
response=urllib.request.urlopen("http://192.164.1.39/cam-hi.jpg")
img=Image.open(BytesIO(response.read(100000)))
try:
output=str(i)+".jpg"
img.save(output)
img.save('C:/Users/hi/Desktop/savings/'+"high.jpg", 'JPEG')
except IOError:
print("cannot convert",infile)
a="finished"
print(a)
# AFTER HERE, THERE İS A LONG CODE FOR IMAGE PROCESSING THAT THE PROGRAM CANT REACH
#code for image processing.......
cond.notify()
cond.release()
def CamRequest():
global a
while(True):
cond.acquire()
if(a=="stop"):
print(a)
cond.wait()
cond.notify()
cond.release()
response=urllib.request.urlopen("http://192.164.1.39/cam-lo.jpg")
img2=Image.open(BytesIO(response.read(100000)))
img2.save('C:/Users/hi/Desktop/savings/'+"1.jpg", 'JPEG')
time.sleep(0.2)
print(a)
This works fine until the line where I take the high quality picture but cant do anything with it in sense processing because the program stops there(I mean it just "waits") . I looked also for "async requests" but in my case I have to use urllib since the response is only an image. It would be great if I could process the high quality images after I have taken them while the low quality images are still being requested.