|
|
import gradio as gr |
|
|
import numpy as np |
|
|
from PIL import Image |
|
|
import os |
|
|
|
|
|
|
|
|
def analyze_rubiks_cube(image): |
|
|
""" |
|
|
Simplified function to analyze Rubik's Cube images |
|
|
""" |
|
|
if image is None: |
|
|
return "Please upload an image", None |
|
|
|
|
|
try: |
|
|
|
|
|
if isinstance(image, np.ndarray): |
|
|
image = Image.fromarray(image) |
|
|
|
|
|
|
|
|
width, height = image.size |
|
|
|
|
|
|
|
|
image_array = np.array(image) |
|
|
|
|
|
|
|
|
colors = { |
|
|
'red': 0, 'green': 0, 'blue': 0, |
|
|
'yellow': 0, 'orange': 0, 'white': 0 |
|
|
} |
|
|
|
|
|
|
|
|
result_text = f""" |
|
|
π² Rubik's Cube Image Analysis Results |
|
|
|
|
|
π Image Information: |
|
|
- Size: {width} x {height} pixels |
|
|
- Format: {image.format if hasattr(image, 'format') else 'PIL Image'} |
|
|
|
|
|
π Detection Status: |
|
|
β
Image uploaded successfully |
|
|
β
Image format is correct |
|
|
β οΈ Complete AI model is still in training |
|
|
|
|
|
π Note: |
|
|
This is a demo version of the Rubik's cube recognition system. |
|
|
The complete RetinaNet model needs to be trained before accurate cube detection can be performed. |
|
|
|
|
|
π Feature Preview: |
|
|
- Cube face detection |
|
|
- Color tile recognition (Red, White, Blue, Orange, Green, Yellow) |
|
|
- 3D position analysis |
|
|
- Cube state evaluation |
|
|
""" |
|
|
|
|
|
return result_text, image |
|
|
|
|
|
except Exception as e: |
|
|
error_msg = f"Error processing image: {str(e)}" |
|
|
return error_msg, image |
|
|
|
|
|
|
|
|
def create_interface(): |
|
|
""" |
|
|
Create Gradio interface |
|
|
""" |
|
|
with gr.Blocks( |
|
|
title="π² Rubik's Cube Recognition System", |
|
|
theme=gr.themes.Soft(), |
|
|
css=""" |
|
|
.gradio-container { |
|
|
max-width: 1200px; |
|
|
margin: auto; |
|
|
} |
|
|
""" |
|
|
) 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;"> |
|
|
Intelligent Rubik's Cube Detection and Analysis Platform Based on Deep Learning |
|
|
</p> |
|
|
</div> |
|
|
""") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(scale=1): |
|
|
gr.Markdown("### π€ Upload Image") |
|
|
|
|
|
input_image = gr.Image( |
|
|
label="Select Rubik's Cube Image", |
|
|
type="pil", |
|
|
height=400 |
|
|
) |
|
|
|
|
|
analyze_btn = gr.Button( |
|
|
"π Start Analysis", |
|
|
variant="primary", |
|
|
size="lg", |
|
|
scale=1 |
|
|
) |
|
|
|
|
|
gr.Markdown(""" |
|
|
### π‘ Usage Tips |
|
|
- Supports JPG, PNG formats |
|
|
- Clear images with good lighting recommended |
|
|
- Better results when cube takes up larger portion of image |
|
|
""") |
|
|
|
|
|
with gr.Column(scale=1): |
|
|
gr.Markdown("### π Analysis Results") |
|
|
|
|
|
result_output = gr.Textbox( |
|
|
label="Detection Report", |
|
|
lines=15, |
|
|
max_lines=20, |
|
|
show_copy_button=True |
|
|
) |
|
|
|
|
|
output_image = gr.Image( |
|
|
label="Processed Image", |
|
|
type="pil", |
|
|
height=400 |
|
|
) |
|
|
|
|
|
gr.HTML(""" |
|
|
<div style="text-align: center; padding: 20px; border-top: 1px solid #eee; margin-top: 20px;"> |
|
|
<h3>π¬ Technical Features</h3> |
|
|
<div style="display: flex; justify-content: space-around; flex-wrap: wrap;"> |
|
|
<div style="margin: 10px;"> |
|
|
<strong>π§ AI Model</strong><br> |
|
|
RetinaNet + SpineNet-49 |
|
|
</div> |
|
|
<div style="margin: 10px;"> |
|
|
<strong>π― Detection Accuracy</strong><br> |
|
|
7-class Object Detection |
|
|
</div> |
|
|
<div style="margin: 10px;"> |
|
|
<strong>β‘ Processing Speed</strong><br> |
|
|
Real-time Inference |
|
|
</div> |
|
|
<div style="margin: 10px;"> |
|
|
<strong>π Deployment Platform</strong><br> |
|
|
Hugging Face Spaces |
|
|
</div> |
|
|
</div> |
|
|
</div> |
|
|
""") |
|
|
|
|
|
|
|
|
analyze_btn.click( |
|
|
fn=analyze_rubiks_cube, |
|
|
inputs=[input_image], |
|
|
outputs=[result_output, output_image] |
|
|
) |
|
|
|
|
|
input_image.change( |
|
|
fn=analyze_rubiks_cube, |
|
|
inputs=[input_image], |
|
|
outputs=[result_output, output_image] |
|
|
) |
|
|
|
|
|
return demo |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo = create_interface() |
|
|
demo.launch( |
|
|
server_name="0.0.0.0", |
|
|
server_port=7860, |
|
|
show_error=True |
|
|
) |
|
|
|