Skip to content

Commit d7469d7

Browse files
committed
rock paper classification using CNN model
1 parent f6aea14 commit d7469d7

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
from flask import Flask, render_template, Response, request, redirect, url_for
2+
import cv2
3+
import os
4+
import numpy as np
5+
import pickle
6+
from PIL import Image
7+
import tensorflow
8+
9+
app = Flask(__name__)
10+
11+
# Initialize the camera
12+
camera = cv2.VideoCapture( 0)
13+
14+
@app.route('/')
15+
def index():
16+
return render_template('index.html')
17+
18+
def generate_frames():
19+
while True:
20+
success, frame = camera.read()
21+
if not success:
22+
break
23+
else:
24+
ret, buffer = cv2.imencode('.jpg', frame)
25+
frame = buffer.tobytes()
26+
yield (b'--frame\r\n'
27+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
28+
29+
import random
30+
31+
def find_winner(computer , human):
32+
if computer == human:
33+
return f"Your Choice {human} , Computer's Choice {computer} \n It's a tie!"
34+
elif (computer == "stone" and human == "scissors") or \
35+
(computer == "paper" and human == "stone") or \
36+
(computer == "scissors" and human == "paper"):
37+
return f"Your Choice {human} , Computer's Choice {computer} \n Computer wins!"
38+
else:
39+
return f"Your Choice {human} , Computer's Choice {computer} \n You won!"
40+
41+
def choose_randomly():
42+
# List of choices: stone, paper, scissors
43+
choices = ["stone", "paper", "scissors"]
44+
45+
# Use random.choice to select a random choice
46+
computer_choice = random.choice(choices)
47+
48+
return computer_choice
49+
def load_model_and_predict():
50+
with open("D:/datasets/rps/model/model.pkl", "rb") as f:
51+
model = pickle.load(f)
52+
53+
target_size = (100, 100)
54+
image_path = "pic//image.jpg"
55+
56+
image = Image.open(image_path)
57+
image = image.resize(target_size)
58+
image = image.convert('L')
59+
image_pixel = np.array(image) / 255
60+
image_pixel = image_pixel.reshape(1,100,100,1)
61+
print("Image Size : " , image_pixel.shape)
62+
63+
pred = model.predict(image_pixel)
64+
pred = np.argmax(pred , axis = 1)
65+
human_choice=None
66+
67+
68+
if (pred == 0):
69+
human_choice='stone'
70+
71+
elif (pred == 1):
72+
human_choice='paper'
73+
74+
else:
75+
human_choice='scissors'
76+
77+
78+
79+
80+
81+
print(model.summary())
82+
83+
return find_winner(choose_randomly() , human_choice)
84+
85+
86+
87+
@app.route('/video_feed')
88+
def video_feed():
89+
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
90+
91+
@app.route('/capture', methods=['POST'])
92+
def capture():
93+
_, frame = camera.read()
94+
if _:
95+
# Generate a unique filename for each captured image
96+
filename = f"pic//image.jpg"
97+
cv2.imwrite(filename, frame)
98+
return render_template('image.html' , size = load_model_and_predict())
99+
else:
100+
return "Failed to capture image."
101+
102+
if __name__ == "__main__":
103+
app.run(debug=True)
104+
105+
# Release the camera when the app is closed
106+
camera.release()
107+
cv2.destroyAllWindows()
75.7 KB
Loading
44.6 KB
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Image Details</title>
5+
</head>
6+
<body>
7+
{{size}}
8+
</body>
9+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Camera Capture</title>
5+
</head>
6+
<body>
7+
<h1>Camera Preview</h1>
8+
<img src="{{ url_for('video_feed') }}" width="640" height="480">
9+
<form action="/capture" method="POST">
10+
<button type="submit">Capture</button>
11+
</form>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)