|
| 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() |
0 commit comments