Error processing frame: failed to allocate memory [Op:AddV2] – Deepface object detection

i’m a newbie to object detection, we wrote a script to identify gender and age of a person using the deepface library and then adds trackers using the Sort library.

The code works well on the CPU and Google Collabs but when i run it on the GPU (RTX 3050) it raises this error:

Error processing frame: {{function_node _wrapped__AddV2_device/job:localhost/replica:0/task:0/device:GPU:0}} failed to allocate memory [Op:AddV2]

I tried setting the experimental.set_memory_growth to True, but it still raised the same error. Task manager showed that the GPU wasn’t being used a 100%, and oddly enough whenever the error occurs the GPU usage goes down to 0%.

Can my GPU not run object detection of this magnitude ? Are there any optimizations that i must employ ?

Source Code:

results = DeepFace.analyze(resized_frame, actions=['gender', 'age'], enforce_detection=True,
                                   detector_backend='retinaface')

        # Process each detected face
        detections = []
        for result in results:
            # Extract bounding box and convert to integers
            x = int(result['region']['x'])
            y = int(result['region']['y'])
            w = int(result['region']['w'])
            h = int(result['region']['h'])
            # confidence = float(result.get('face_confidence', 0.5))  # Default to 0.5 if missing

            # Add detection for SORT tracker
            if is_within_roi(x, y, w, h):
                confidence = float(result.get('face_confidence', 0.5))
                detections.append([x, y, x + w, y + h, confidence])

        # Ensure detections array is not empty
        if detections:
            # Update tracker with valid detections
            tracked_objects = tracker.update(np.array(detections))
        else:
            tracked_objects = []

        # Iterate through tracked objects
        for track in tracked_objects:
            track_id = int(track[4])  # Unique ID assigned by tracker
            x1, y1, x2, y2 = map(int, track[:4])

            # Match tracker object with DeepFace result (basic IOU/position check)
            for result in results:
                rx, ry, rw, rh = map(int, [result['region']['x'], result['region']['y'], result['region']['w'],
                                           result['region']['h']])
                if abs(x1 - rx) < 20 and abs(y1 - ry) < 20:  # Adjust threshold if necessary
                    # Retrieve gender confidence scores
                    male_confidence = result['gender']['Man']
                    female_confidence = result['gender']['Woman']

                    # Retrieve age from DeepFace result
                    age = int(result.get('age', -1))  # Default to -1 if age is missing
                    age_range = get_age_range(age)
                    gender = "Male" if male_confidence > 99.8 else "Female" if female_confidence > 60 else "Unknown"


                    if track_id not in tracking_data:
                        tracking_data[track_id] = {
                            "Ages": [],
                            "Genders": set(),
                            "Entry Time": formatted_time,
                            "Exit Time": None
                        }
                    tracking_data[track_id]["Ages"].append(age)
                    tracking_data[track_id]["Genders"].add(gender)

                    if is_within_roi(x1, y1, x2 - x1, y2 - y1):
                        tracking_data[track_id]["Exit Time"] = formatted_time

                    if gender != "Unknown":
                        label = f"{gender}, Age: {age_range}"
                        color = (0, 255, 0)
                        draw_label(resized_frame, x1, y1, x2 - x1, y2 - y1, label, color)

                    if gender == "Male":
                        global_male_count.add(track_id)
                        male_ages.append(age)
                    elif gender == "Female":
                        global_female_count.add(track_id)
                        female_ages.append(age)

                    # Draw the label on the frame
                    draw_label(resized_frame, x1, y1, x2 - x1, y2 - y1, label, color)

Thank you, and any insights would be super helpful

New contributor

Leroy Jeslyn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật