shireenchand's picture
Update app.py
856e4ad
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)