If you want to get the coordinates of one detection you can use -ext_output
flag of AlexeyAB/darknet code as @Hadi said:
./darknet detector test data/obj.data cfg/yolov4.cfg yolov4.weights -ext_output data/person.jpg
Or you can save it directly on a text file:
./darknet detector test data/obj.data cfg/yolov4.cfg yolov4.weights -ext_output data/person.jpg > output.txt
But if you have a big number of images, you need to detect them all at once and save it on a JSON file:
./darknet detector test data/obj.data cfg/yolov4.cfg yolov4.weights -ext_output -out train.json < train.txt
Where train.txt
is the file where you have the directories of all your images you want to detect and train.json
is the JSON file where the results are saved.
train.json
file will look like this:
[
{
"frame_id":1,
"filename":"data/dataset/val/dog/image.png",
"objects": [
{"class_id":0, "name":"dog", "relative_coordinates":{"center_x":0.452191, "center_y":0.809318, "width":0.349666, "height":0.378723}, "confidence":0.418734},
{"class_id":1, "name":"cat", "relative_coordinates":{"center_x":0.454491, "center_y":0.891459, "width":0.397718, "height":0.220163}, "confidence":0.024015}
]
},
{
"frame_id":2,
"filename":"data/dataset/val/dog/image2.jpg",
"objects": [
{"class_id":0, "name":"dog", "relative_coordinates":{"center_x":0.444495, "center_y":0.539488, "width":0.297957, "height":0.307668}, "confidence":0.991456}
]
},
...
...
]
And to access the values, you can use this code:
import json
def get_data(distros_dict):
json_to_variable = []
# For every frame.
for distro in distros_dict:
filename = distro['filename']
if len(distro['objects']) != 0:
# For every detection.
for obj in range(len(distro['objects'])):
# Get values.
frame_id = distro['frame_id']
class_id = distro['objects'][obj]["class_id"]
x = distro['objects'][obj]["relative_coordinates"]["center_x"]
y = distro['objects'][obj]["relative_coordinates"]["center_y"]
width = distro['objects'][obj]["relative_coordinates"]["width"]
height = distro['objects'][obj]["relative_coordinates"]["height"]
confidence = distro['objects'][obj]["confidence"]
# And save them.
print(f"{frame_id} {class_id} {x} {y} {width} {height} {confidence}")
json_to_variable.append([frame_id, class_id, x, y, width, height, confidence])
# If you need to use json_to_variable here, move "json_to_variable = []" inside "for distro in distros_dict:"
# Add your code here.
# Or return json_to_variable to use it outside this function.
with open('train.json', 'r') as f:
distros_dict_train = json.load(f)
with open('test.json', 'r') as f:
distros_dict_test = json.load(f)
get_data(distros_dict_train)
get_data(distros_dict_test)
Another option is to import daknet
like is mentioned here. The functions called here are the ones that you can find in the darknet.py
file, but I think that the functions have been changed.