Python Dictionaries and Django Views

Hello! I am using Django to create a webpage showing a webcam feed with an object detection model and a counter for detected objects.

:white_check_mark: Show the camera feed to my page.
:white_check_mark: Implement an object detection model.
:white_large_square: Display a counter for the detected objects.

The counter data is implemented with the object detection model in a python file named with the following code:

class ObjectDetector(object):
    def __init__(self, pretrained_model=None):    
        if pretrained_model is None: 
            self.pretrained_model = model_path
        self.model = YOLO(self.pretrained_model)

    def _run_inference(self, frame):
        results = json.loads(self.model(frame)[0].tojson())
        detected_objects = [label['name'] for label in results]
        return Counter(detected_objects)

The returned value Counter(detected_objects) is a dictionary listing object names and their respective counts.

I’m looking for the idea of making this concept work on my label.html.

def objects(request):
	object_list = ObjectDetector._run_inference()
	return render(request, 'objectdetection/label.html', {{ object_list }})


    <div class="list">
        <ul class="objects">
            {% for key in object_list.keys( %}
            <li class=""> {key}: {object_list[key]} </li>
            {% endfor %}

What I’m trying to achieve is something like:

object_list = Counter(detected_objects)

Counter({‘person’: 1, ‘chair’: 3})

for key in object_list.keys():
  print(f"{key}: {object_list[key]}")

person: 1
chair: 3

Please guide me through the concepts of the steps necessary to achieve this goal or issues with my thought process in tackling this problem. Thank you.

Review the docs for templates, specifically the section on variables.

Beyond that, you’ll need to be more specific about what problems or issues you’re facing here.

1 Like