Download app.py from Lenskart/eye_test: direct link, hf CLI and curl.
- Browser
- Download file 2.83 kB
-
https://huggingface.co/spaces/Lenskart/eye_test/resolve/main/app.py
- Command line
-
hf download hf://spaces/Lenskart/eye_test/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Lenskart/eye_test/resolve/main/app.py
2.83 kB
| import gradio as gr | |
| import pandas as pd | |
| import os | |
| import cv2 | |
| import tempfile | |
| # --- Paths --- | |
| GT_FILE = "data/train_logs.csv" # Ground truth lens changes | |
| LEADERBOARD_FILE = "leaderboard.csv" | |
| # Load ground truth | |
| if os.path.exists(GT_FILE): | |
| gt_df = pd.read_csv(GT_FILE) | |
| else: | |
| gt_df = pd.DataFrame(columns=["frame", "lens"]) | |
| # Initialize leaderboard if not exists | |
| if not os.path.exists(LEADERBOARD_FILE): | |
| pd.DataFrame(columns=["Name", "Score"]).to_csv(LEADERBOARD_FILE, index=False) | |
| # --- Simple lens detection placeholder --- | |
| # Replace with your actual detection logic | |
| def detect_lens_changes(video_path): | |
| cap = cv2.VideoCapture(video_path) | |
| total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | |
| preds = pd.DataFrame({"frame": range(total_frames), "lens": ["A"]*total_frames}) | |
| cap.release() | |
| return preds | |
| # --- Evaluation function --- | |
| def evaluate_video(video_file, name): | |
| try: | |
| tmp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") | |
| tmp_video.write(video_file.read()) | |
| tmp_video.close() | |
| pred_df = detect_lens_changes(tmp_video.name) | |
| merged = pd.merge(gt_df, pred_df, on="frame", how="left") | |
| if merged.empty or "lens_x" not in merged.columns or "lens_y" not in merged.columns: | |
| return "Error: Ground truth or prediction missing columns.", pd.read_csv(LEADERBOARD_FILE) | |
| merged.fillna("", inplace=True) | |
| score = (merged["lens_x"] == merged["lens_y"]).mean() | |
| score = round(float(score), 4) | |
| # Update leaderboard | |
| lb_df = pd.read_csv(LEADERBOARD_FILE) | |
| lb_df.loc[len(lb_df)] = [name, score] | |
| lb_df = lb_df.sort_values("Score", ascending=False).reset_index(drop=True) | |
| lb_df.to_csv(LEADERBOARD_FILE, index=False) | |
| return f"Your Score: {score}", lb_df | |
| except Exception as e: | |
| return f"Error: {str(e)}", pd.read_csv(LEADERBOARD_FILE) | |
| # --- Instructions --- | |
| instructions = """ | |
| # 🏆 Video Lens Detection Hackathon | |
| **Instructions:** | |
| 1. Download dataset from `data/` folder. | |
| 2. Videos contain eye test recordings. | |
| 3. Train your model locally to detect lens changes per frame. | |
| 4. Upload your video predictions here. | |
| 5. Enter your name to appear on the leaderboard. | |
| """ | |
| # --- Gradio UI --- | |
| with gr.Blocks() as demo: | |
| gr.Markdown(instructions) | |
| with gr.Row(): | |
| name_input = gr.Textbox(label="Your Name") | |
| video_input = gr.File(label="Upload your video", file_types=[".mp4"]) | |
| output_text = gr.Textbox(label="Score / Error", interactive=False) | |
| leaderboard = gr.Dataframe(pd.read_csv(LEADERBOARD_FILE), label="Leaderboard") | |
| submit_btn = gr.Button("Submit") | |
| submit_btn.click( | |
| fn=evaluate_video, | |
| inputs=[video_input, name_input], | |
| outputs=[output_text, leaderboard] | |
| ) | |
| demo.launch() | |