Spaces:
Sleeping
Sleeping
Commit
·
f016799
1
Parent(s):
17e3cbf
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
import keras
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
import random
|
| 6 |
+
|
| 7 |
+
classes = ['paper', 'rock', 'scissors']
|
| 8 |
+
|
| 9 |
+
model = tf.keras.models.load_model('rps.h5')
|
| 10 |
+
|
| 11 |
+
def classify_image(inp):
|
| 12 |
+
img = cv2.resize(inp,(224,224),interpolation=cv2.INTER_AREA)
|
| 13 |
+
img = np.reshape(img,(1,224,224,3))
|
| 14 |
+
pred = model1.predict(img).flatten()
|
| 15 |
+
confidences = {classes[i]: float(pred[i]) for i in range(3)}
|
| 16 |
+
global det
|
| 17 |
+
det = classes[pred.argmax(axis=-1)]
|
| 18 |
+
print(det)
|
| 19 |
+
return confidences
|
| 20 |
+
|
| 21 |
+
def random_char(n):
|
| 22 |
+
print("HELLO")
|
| 23 |
+
n = random.randint(0,2)
|
| 24 |
+
global sel
|
| 25 |
+
sel = classes[n]
|
| 26 |
+
print(sel)
|
| 27 |
+
return classes[n].upper()
|
| 28 |
+
|
| 29 |
+
def result(t):
|
| 30 |
+
print("HIOAL")
|
| 31 |
+
print(sel, det)
|
| 32 |
+
if (sel == 'rock' and det == 'paper') or (sel == 'paper' and det == 'scissors') or (sel == "scissors" and det == "rock"):
|
| 33 |
+
return "YOU WON"
|
| 34 |
+
elif (sel == 'paper' and det == 'rock') or (sel == 'scissors' and det == 'paper') or (sel == "rock" and det == "scissors"):
|
| 35 |
+
return "YOU LOST"
|
| 36 |
+
else:
|
| 37 |
+
return "IT'S A TIE"
|
| 38 |
+
|
| 39 |
+
import gradio as gr
|
| 40 |
+
|
| 41 |
+
webcam = gr.inputs.Image(shape=(224, 224), source="webcam")
|
| 42 |
+
classify = gr.Interface(fn=classify_image,
|
| 43 |
+
inputs=webcam,
|
| 44 |
+
outputs=gr.Label(num_top_classes=3))
|
| 45 |
+
|
| 46 |
+
computer = gr.Interface(fn=random_char,
|
| 47 |
+
inputs=None,
|
| 48 |
+
outputs=gr.TextArea(max_lines=1,label='The computer selected:'))
|
| 49 |
+
|
| 50 |
+
final = gr.Interface(fn=result,
|
| 51 |
+
inputs=None,
|
| 52 |
+
outputs=gr.TextArea(max_lines=1,label='Result'))
|
| 53 |
+
|
| 54 |
+
final = gr.Parallel(classify, computer, final).launch(debug=True)
|