Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import torch #2.0.1
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
import random
|
| 8 |
+
import string
|
| 9 |
+
from imageAI import SimpleModel
|
| 10 |
+
from myImage import ImageToCV,CVtoImage
|
| 11 |
+
IMAGE_SIZE = 64
|
| 12 |
+
|
| 13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
model = SimpleModel(path='best_model.pth').to(device)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def predict_and_draw(input_image):
|
| 18 |
+
#input_image=ImageToCV(input_image)
|
| 19 |
+
|
| 20 |
+
instructions = model.predict(input_image)
|
| 21 |
+
img = cv2.imread(input_image, cv2.IMREAD_GRAYSCALE)
|
| 22 |
+
img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE))
|
| 23 |
+
# 創建一個新的白色圖像
|
| 24 |
+
image = np.zeros((IMAGE_SIZE, IMAGE_SIZE), dtype=np.uint8)
|
| 25 |
+
|
| 26 |
+
# 執行每條繪圖指令
|
| 27 |
+
for instruction in instructions:
|
| 28 |
+
try:exec(instruction)
|
| 29 |
+
except:
|
| 30 |
+
import traceback
|
| 31 |
+
traceback.print_exc()
|
| 32 |
+
#image=CVtoImage(image)
|
| 33 |
+
return img, image, "\n".join(instructions)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=predict_and_draw,
|
| 38 |
+
inputs=gr.Image(type="filepath"),
|
| 39 |
+
outputs=[ gr.Image(type="numpy" ,image_mode="L" ,label="Input Image"), gr.Image(type="numpy" ,image_mode="L" ,label="Output Image"), gr.Textbox(label="Generated Instructions")],
|
| 40 |
+
title="Image to Drawing Instructions",
|
| 41 |
+
description="Upload an image, and the model will predict drawing instructions based on it."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
iface.launch()
|