I want to read the output of the command line while running an object detection algorithm to actively monitor for a certain object. I tried this:
import subprocess
cmd = './darknet detector test data/obj.data cfg/yolov3_testing.cfg yolov3_training_2.weights data/fire.jpg -thresh 0.02'
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while proc.poll() is None:
output = proc.stdout.readline() #get line from output
out = output.decode('UTF-8') #convert output from type byte to string
print(out)
Which kinda works but after reading a hundred or so lines and outputting them, it gets stuck and stops printing lines or executing any code in the loop. Also, most of the lines are read as errors (go to sterr instead of stdout). When terminating darknet, the rest of the lines are immediately printed. This makes me think that while the loop is stuck, the results of the code are actually being buffered, then executed on termination.
Here is the output before terminating darknet (without piping stderr):
And after terminating darknet:
Other methods I tried are:
from subprocess import Popen, PIPE
cmd = './darknet detector test data/obj.data cfg/yolov3_testing.cfg yolov3_training_2.weights data/fire.jpg -thresh 0.02'
with Popen(["./darknet", "detector", "test", "data/obj.data", "cfg/yolov3_testing.cfg", "yolov3_training_2.weights", "data/fire.jpg", "-thresh", "0.01"], stdout=PIPE, stderr=PIPE, bufsize=2, universal_newlines=True) as p:
for line in p.stdout:
print(line, end='')
and
subprocess.check_call(["./darknet", "detector", "test", "data/obj.data", "cfg/yolov3_testing.cfg", "yolov3_training_2.weights", "data/fire.jpg", "-t
These all have the same problem.