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