as part of a project I am doing wity Yolov4, I am trying to test multiple images with learned weights and copy the resulting images with added bounding boxes to my google drive. The following is the code I am running on colab. It just isn’t working, and there is no log messages to debug. Please not if I run the command in the cmd variable directly, it is working fine, but that’s just for one image. My aim is to automate or “batch” the testing process.
Thanks for your help.
JavaScript
x
17
17
1
#%%capture
2
import os,sys
3
import subprocess
4
directory = '/content/darknet/data/testimages/'
5
for filename in os.listdir(directory):
6
#cmd = "./darknet detector test data/obj.data cfg/yolov4-obj.cfg /content/darknet/cfg/yolov4-obj_4000.weights /content/darknet/data/testimages/" + filename
7
cmd = "!./darknet detector test data/obj.data cfg/yolov4-obj.cfg /content/darknet/cfg/yolov4-obj_4000.weights /content/darknet/data/testimages/" + filename
8
try:
9
subprocess.run([sys.executable,cmd])
10
except:
11
print("failed")
12
#print(os.path.join(directory, filename))
13
cmd = "!cp 'predictions.jpg' '/mydrive/birds/results/" + filename + "_results.jpg' "
14
#print(cmd)
15
#cmd = "ls -ltr"
16
subprocess.run([sys.executable,cmd])
17
Advertisement
Answer
After a few hours of trial and error, a simple documentation search on Colab’s page gave me the answer. It was under the heading “passing values to and from the shell”. The following modified code does the trick for me.
JavaScript
1
13
13
1
%%capture
2
import os,sys
3
import subprocess
4
directory = '/content/darknet/data/testimages/'
5
for filename in os.listdir(directory):
6
cmd = "./darknet detector test data/obj.data cfg/yolov4-obj.cfg /content/darknet/cfg/yolov4-obj_4000.weights /content/darknet/data/testimages/" + filename
7
try:
8
!{cmd}
9
except:
10
print("failed")
11
cmd = "cp 'predictions.jpg' '/mydrive/birds/results/" + filename + "_results.jpg' "
12
!{cmd}
13