Spaces:
Sleeping
Sleeping
Download app.py from WahibR/Phone_Detector: direct link, hf CLI and curl.
- Browser
- Download file 3.59 kB
-
https://huggingface.co/spaces/WahibR/Phone_Detector/resolve/main/app.py
- Command line
-
hf download hf://spaces/WahibR/Phone_Detector/app.py
-
curl -L -o app.py https://huggingface.co/spaces/WahibR/Phone_Detector/resolve/main/app.py
3.59 kB
| import base64 | |
| import gradio as gr | |
| from openai import OpenAI | |
| from PIL import Image, ImageDraw | |
| client = OpenAI() # API Key is auto-loaded from HF Secrets | |
| # ---------------------------------------------------- | |
| # SUPER PROMPT β HIGH RECALL SCRATCH / CRACK DETECTION | |
| # ---------------------------------------------------- | |
| DETECTION_PROMPT = """ | |
| You are an expert smartphone surface inspection system. | |
| Your job is to detect **EVERY visible defect**, even faint ones. | |
| OUTPUT FORMAT (strict JSON only): | |
| { | |
| "items": [ | |
| { | |
| "type": "scratch_big" | "scratch_medium" | "scratch_micro" | "crack" | "shatter" | "dent", | |
| "confidence": 0.0, | |
| "box": [x1, y1, x2, y2] | |
| } | |
| ] | |
| } | |
| DEFINITIONS: | |
| - scratch_big: long/wide scratch visible in normal light. | |
| - scratch_medium: clearly visible scratch but not deep. | |
| - scratch_micro: thin/light scratch visible only under strong light. | |
| - crack: fracture in glass. | |
| - shatter: spiderweb fractures. | |
| - dent: deformation in metal frame. | |
| RULES: | |
| 1. **Detect ALL scratches** including faint, thin, hairline ones. | |
| 2. NEVER skip a defect. If unsure β include with lower confidence. | |
| 3. If you see multiple lines β output multiple boxes. | |
| 4. Long scratches β output multiple small boxes, not one large box. | |
| 5. Boxes must tightly fit the scratch area. | |
| 6. Only output JSON β no text, no explanation. | |
| """ | |
| # ---------------------------------------------------- | |
| # Helper: Encode image as Base64 | |
| # ---------------------------------------------------- | |
| def encode_image(image): | |
| _, buffer = image.save("temp.jpg", format="JPEG"), image | |
| with open("temp.jpg", "rb") as f: | |
| return base64.b64encode(f.read()).decode() | |
| # ---------------------------------------------------- | |
| # Function: Run OpenAI Vision & draw boxes | |
| # ---------------------------------------------------- | |
| def analyze_and_draw(image): | |
| # Encode image for API | |
| buffered = encode_image(image) | |
| # CALL OPENAI VISION API | |
| response = client.responses.create( | |
| model="gpt-4o-mini", | |
| input=[ | |
| { | |
| "role": "user", | |
| "content": [ | |
| { | |
| "type": "input_text", | |
| "text": DETECTION_PROMPT | |
| }, | |
| { | |
| "type": "input_image", | |
| "image_url": f"data:image/jpeg;base64,{buffered}" | |
| } | |
| ] | |
| } | |
| ] | |
| ) | |
| # Extract JSON string | |
| json_text = response.output[0].content[0].text | |
| # TRY to load JSON | |
| import json | |
| try: | |
| data = json.loads(json_text) | |
| except: | |
| return image, {"error": "JSON parsing failed", "raw": json_text} | |
| # Draw boxes | |
| draw = ImageDraw.Draw(image) | |
| for item in data.get("items", []): | |
| box = item["box"] | |
| defect_type = item["type"] | |
| # Draw rectangle | |
| draw.rectangle(box, outline="red", width=4) | |
| # Label | |
| draw.text((box[0], box[1] - 10), defect_type, fill="red") | |
| return image, data | |
| # ---------------------------------------------------- | |
| # GRADIO APP | |
| # ---------------------------------------------------- | |
| iface = gr.Interface( | |
| fn=analyze_and_draw, | |
| inputs=gr.Image(type="pil", label="Upload Phone Image"), | |
| outputs=[ | |
| gr.Image(type="pil", label="Annotated Result"), | |
| gr.JSON(label="Analysis Details") | |
| ], | |
| title="OpenAI Vision β Phone Scratch & Crack Detector", | |
| description="Uploads an image β detects ALL scratches, cracks, dents with bounding boxes using GPT-4o Vision." | |
| ) | |
| iface.launch() | |