|
import gradio as gr |
|
import numpy as np |
|
from PIL import Image |
|
import os |
|
|
|
|
|
CATEGORY_INDEX = { |
|
1: {'id': 1, 'name': 'face'}, |
|
2: {'id': 2, 'name': 'red_tile'}, |
|
3: {'id': 3, 'name': 'white_tile'}, |
|
4: {'id': 4, 'name': 'blue_tile'}, |
|
5: {'id': 5, 'name': 'orange_tile'}, |
|
6: {'id': 6, 'name': 'green_tile'}, |
|
7: {'id': 7, 'name': 'yellow_tile'} |
|
} |
|
|
|
def predict_image(image): |
|
""" |
|
Make predictions on input image - Demo version |
|
""" |
|
if image is None: |
|
return "Please upload an image", None |
|
|
|
try: |
|
|
|
if isinstance(image, np.ndarray): |
|
image = Image.fromarray(image) |
|
|
|
|
|
width, height = image.size |
|
|
|
|
|
result_text = f"""π² Rubik's Cube Analysis Results |
|
|
|
π Image Information: |
|
- Dimensions: {width} Γ {height} pixels |
|
- Format: {getattr(image, 'format', 'PIL Image')} |
|
|
|
π Detection Status: |
|
β
Image uploaded successfully |
|
β
Image format is valid |
|
β οΈ AI model is currently in development |
|
|
|
π Demo Mode: |
|
This is a preview of the Rubik's cube recognition system. |
|
The complete RetinaNet model will detect: |
|
|
|
π― Target Detection Classes: |
|
- Cube faces |
|
- Red tiles |
|
- White tiles |
|
- Blue tiles |
|
- Orange tiles |
|
- Green tiles |
|
- Yellow tiles |
|
|
|
π Coming Soon: |
|
- Real-time object detection |
|
- Bounding box visualization |
|
- Confidence scores |
|
- 3D cube state analysis |
|
""" |
|
|
|
return result_text, image |
|
|
|
except Exception as e: |
|
error_msg = f"Error processing image: {str(e)}\n\nThis is a demo version." |
|
return error_msg, image |
|
|
|
def create_demo(): |
|
"""Create the Gradio interface""" |
|
|
|
with gr.Blocks( |
|
title="π² Rubik's Cube Recognition System", |
|
theme=gr.themes.Soft() |
|
) as demo: |
|
|
|
gr.HTML(""" |
|
<div style="text-align: center; padding: 20px;"> |
|
<h1>π² Rubik's Cube Recognition System</h1> |
|
<p style="font-size: 18px; color: #666;"> |
|
Deep Learning-based Rubik's Cube Detection using RetinaNet Architecture |
|
</p> |
|
</div> |
|
""") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
gr.Markdown("### π€ Upload Image") |
|
|
|
input_image = gr.Image( |
|
label="Upload Rubik's Cube Image", |
|
type="pil", |
|
height=350 |
|
) |
|
|
|
analyze_btn = gr.Button( |
|
"π Analyze Image", |
|
variant="primary", |
|
size="lg" |
|
) |
|
|
|
gr.Markdown(""" |
|
### π‘ Tips |
|
- Upload clear images of Rubik's cubes |
|
- Good lighting recommended |
|
- JPG/PNG formats supported |
|
""") |
|
|
|
with gr.Column(scale=1): |
|
gr.Markdown("### π Analysis Results") |
|
|
|
result_text = gr.Textbox( |
|
label="Detection Report", |
|
lines=12, |
|
max_lines=15, |
|
show_copy_button=True |
|
) |
|
|
|
output_image = gr.Image( |
|
label="Processed Image", |
|
type="pil", |
|
height=350 |
|
) |
|
|
|
|
|
analyze_btn.click( |
|
fn=predict_image, |
|
inputs=[input_image], |
|
outputs=[result_text, output_image] |
|
) |
|
|
|
input_image.change( |
|
fn=predict_image, |
|
inputs=[input_image], |
|
outputs=[result_text, output_image] |
|
) |
|
|
|
gr.HTML(""" |
|
<div style="text-align: center; padding: 20px; margin-top: 20px; border-top: 1px solid #eee;"> |
|
<p><strong>π¬ Technology Stack:</strong> TensorFlow β’ RetinaNet β’ SpineNet-49 β’ Gradio</p> |
|
<p><strong>π§ Contact:</strong> <a href="https://huggingface.co/itsyuimorii">@itsyuimorii</a></p> |
|
</div> |
|
""") |
|
|
|
return demo |
|
|
|
|
|
if __name__ == "__main__": |
|
demo = create_demo() |
|
demo.launch( |
|
server_name="0.0.0.0", |
|
server_port=7860, |
|
show_error=True |
|
) |
|
|