Skip to content

Fix: Resolve MVP pipeline ZeroDivisionError & optimize hardware utilization via TensorFlow fallback #13

Description

@Kunal-Somani

Hi team!

While setting up the project locally to test the emotion detection pipeline, I audited the mvp.py script and the dependency tree. I noticed two areas where we can significantly improve local stability and hardware utilization.

Problem 1: MVP Pipeline Crash (ZeroDivisionError)

The mvp.py script is a great standalone tool for local testing without Firebase credentials. However, it currently lacks a safety net for edge cases. If cv2.VideoCapture fails to process any frames (e.g., the video file is missing, corrupted, or no faces are detected), the predictions array remains empty ([]).

When this empty array is passed to getPercentages(), the script crashes hard on line 60 because it attempts to divide by len(predictions) (which is 0):

'Angry': round((emotionCountMap["Angry"] / len(predictions) * 100), 2) # ZeroDivisionError

Additionally, main() accepts a video_path argument, but hardcodes video = cv2.VideoCapture("fv.mp4") internally, breaking dynamic video loading.

Problem 2: Hardware Utilization Bottleneck

Currently, the project strictly requires tensorflow-cpu = "^2.18.0" in both pyproject.toml and the Environment section of the README.md. This entirely locks out users with CUDA-enabled hardware from utilizing GPU acceleration, artificially slowing down local emotion analysis.

Proposed Solution

I have a local branch ready that addresses both issues simultaneously:

1. Pipeline Stability (mvp.py):
• Wired the dynamic video_path argument directly into the cv2.VideoCapture() call.
• Introduced an early-exit guard clause in getPercentages() to handle empty predictions safely:

if not predictions:
    print("No predictions generated. Returning empty percentages.")
    return {}

2. Hardware Optimization (pyproject.toml & README.md):
• Swapped tensorflow-cpu for the standard tensorflow package. The standard package natively utilizes GPUs if available, and automatically falls back to CPU if no GPU is found, making it strictly superior and universally compatible.
• Updated the README.md Environment section to reflect the removal of the CPU-only restriction.

Benefits

Zero-Crash Local Testing: Developers can test edge-case videos (no faces, corrupt files) without crashing the Python process.
Massive Speed Boost: Users with dedicated GPUs will see significantly faster frame inference times.
Fully Backward Compatible: CPU-only users will experience no change in behavior due to TensorFlow's native fallback mechanism.

If this aligns with the project's development goals, please let me know if I have the green light to go ahead and open the PR!

cc: @jvJUCA @marcgc21 @KarinePistili

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions