arham061 commited on
Commit
4c00ae9
1 Parent(s): af9b471

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -15
app.py CHANGED
@@ -1,13 +1,14 @@
1
  from fastai.vision.all import *
2
  import gradio as gr
 
3
  import cv2
4
 
5
- __all__ = ['is_rock', 'learn', 'classify_images', 'get_webcam_image' 'categories', 'image', 'label', 'examples', 'intf']
6
 
7
- def is_rock(x): return x[0].issuper()
8
-
9
- learn = load_learner('RPS_model2.pkl')
10
 
 
11
 
12
  categories = ('paper', 'rock', 'scissors')
13
 
@@ -15,16 +16,23 @@ def classify_images(img):
15
  pred, idx, probs = learn.predict(img)
16
  return dict(zip(categories, map(float, probs)))
17
 
18
- #def get_webcam_image():
19
- # cap = cv2.VideoCapture(0)
20
- # _, frame = cap.read()
21
- # cap.release()
22
- # return frame
23
-
24
-
25
- image = gr.Webcam()
 
 
 
 
 
 
 
26
  label = gr.outputs.Label()
27
- examples = ['rock.jpg', 'paper.jpg', 'scissor.jpg']
28
-
29
- intf = gr.Interface(fn = classify_images, inputs = image, outputs = label, examples = examples).launch()
30
 
 
 
 
1
  from fastai.vision.all import *
2
  import gradio as gr
3
+ import random
4
  import cv2
5
 
6
+ __all__ = ['is_rock', 'learn', 'classify_image', 'determine_winner', 'play game' 'categories', 'image', 'label', 'examples', 'intf']
7
 
8
+ def is_rock(x):
9
+ return x[0].issuper()
 
10
 
11
+ learn = load_learner('rps_model.pkl')
12
 
13
  categories = ('paper', 'rock', 'scissors')
14
 
 
16
  pred, idx, probs = learn.predict(img)
17
  return dict(zip(categories, map(float, probs)))
18
 
19
+ def determine_winner(user_choice, computer_choice):
20
+ if user_choice == computer_choice:
21
+ return "It's a tie!"
22
+ elif (user_choice == 'rock' and computer_choice == 'scissors') or (user_choice == 'paper' and computer_choice == 'rock') or (user_choice == 'scissors' and computer_choice == 'paper'):
23
+ return "You won!"
24
+ else:
25
+ return "Computer won!"
26
+
27
+ def play_game(img):
28
+ user_choice = img
29
+ computer_choice = random.choice(categories)
30
+ winner = determine_winner(user_choice, computer_choice)
31
+ return f"User's choice: {user_choice}\nComputer's choice: {computer_choice}\n{winner}"
32
+
33
+ image = gr.inputs.Image(shape=(192, 192))
34
  label = gr.outputs.Label()
35
+ examples = ['rock.jpg', 'paper.jpg', 'scissors.jpg']
 
 
36
 
37
+ intf = gr.Interface(fn=play_game, inputs=image, outputs=label, examples=examples)
38
+ intf.launch(inline=False)