angeloqq commited on
Commit
df67334
·
verified ·
1 Parent(s): 55e8284

Upload 8 files

Browse files
app/app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify, render_template
2
+ from flask_cors import CORS
3
+ import cv2
4
+ import easyocr
5
+ import numpy as np
6
+ import tensorflow as tf
7
+
8
+ app = Flask(__name__)
9
+ CORS(app)
10
+
11
+ # Load your pre-trained model and set up the test set for phone recognition
12
+ test_set = tf.keras.utils.image_dataset_from_directory(
13
+ "/app/testing_data",
14
+ labels="inferred",
15
+ label_mode="categorical",
16
+ class_names=None,
17
+ color_mode="rgb",
18
+ batch_size=32,
19
+ image_size=(64, 64),
20
+ shuffle=True,
21
+ seed=None,
22
+ validation_split=None,
23
+ subset=None,
24
+ interpolation="bilinear",
25
+ follow_links=False,
26
+ crop_to_aspect_ratio=False
27
+ )
28
+
29
+ # Load the CNN model for phone recognition
30
+ cnn = tf.keras.models.load_model("/app/trained_model (1).h5")
31
+
32
+ # Instantiate text reader for OCR
33
+ picture_read = easyocr.Reader(["en", "ar"], gpu=False)
34
+
35
+ def get_class_names():
36
+ # Load the class names from the test_set
37
+ return test_set.class_names
38
+
39
+ class_names = get_class_names()
40
+
41
+ # Route for rendering the index.html template
42
+ @app.route("/")
43
+ def index():
44
+ # Pass the class names to the HTML template
45
+ return render_template("index.html", class_names=class_names)
46
+
47
+ # Route for phone recognition and processing image
48
+ @app.route("/process_image", methods=["POST"])
49
+ def process_image():
50
+ # Get the uploaded file from the request
51
+ uploaded_file = request.files["file"]
52
+
53
+ # Save the file to a temporary location
54
+ temp_file_path = "/app/temp_image.png"
55
+ uploaded_file.save(temp_file_path)
56
+
57
+ # Read and preprocess the image for phone model recognition
58
+ img = cv2.imread(temp_file_path)
59
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
60
+ img = cv2.resize(img, (64, 64))
61
+
62
+ # Make predictions for phone model
63
+ input_arr = np.array([img])
64
+ predictions = cnn.predict(input_arr)
65
+
66
+ # Get the index with the highest probability
67
+ result_index = np.argmax(predictions)
68
+
69
+ # Display the predicted class for phone model
70
+ result_phone_model = class_names[result_index]
71
+
72
+ # Process image using OCR
73
+ picture_results = picture_read.readtext(temp_file_path)
74
+ results_ocr = []
75
+
76
+ conf_threshold = 0.2
77
+ for y in picture_results:
78
+ if y[2] > conf_threshold:
79
+ text = y[1]
80
+ results_ocr.append({"text": text})
81
+
82
+ # Return JSON response with the results
83
+ return jsonify({"result_phone_model": result_phone_model, "results_ocr": results_ocr})
84
+
85
+ if __name__ == "__main__":
86
+ app.run(host="0.0.0.0", port=8000, debug=True)
app/static/images/background.png ADDED
app/static/images/brandName.png ADDED
app/static/images/chat.png ADDED
app/static/images/detectPhone.png ADDED
app/static/images/errorScreenshot.png ADDED
app/static/myStyles.css ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ background-image: url(images/background.png);
3
+ background-size: cover;
4
+ background-repeat: no-repeat;
5
+ display: flex;
6
+ flex-direction: column;
7
+ align-items: center;
8
+ justify-content: center;
9
+ height: 100vh;
10
+ margin: 0;
11
+ }
12
+
13
+ .brand-container {
14
+ display: flex;
15
+ flex-direction: column;
16
+ align-items: center;
17
+ margin-top: 13%;
18
+ text-align: center;
19
+ }
20
+
21
+ .brand-container img {
22
+ display: block;
23
+ width: 130%;
24
+ max-width: 1500px;
25
+ }
26
+
27
+ span {
28
+ color: #f3f3f3;
29
+ font-family: Arial, Helvetica, sans-serif;
30
+ font-size: 30px;
31
+ margin-top: 5%;
32
+ }
33
+
34
+ .buttons-container {
35
+ display: flex;
36
+ align-items: center;
37
+ justify-content: center;
38
+ padding-right: 0;
39
+ margin-top: 10%;
40
+ }
41
+
42
+ label {
43
+ cursor: pointer;
44
+ margin: -3px;
45
+ }
46
+
47
+ input[type="file"] {
48
+ display: none;
49
+ margin: 0;
50
+ padding: 0;
51
+ }
52
+
53
+ button {
54
+ display: none;
55
+ }
56
+
57
+ label img {
58
+ transition: transform 0.2s ease-in-out;
59
+ }
60
+
61
+ label:hover img {
62
+ transform: scale(1.1);
63
+ }
64
+ /*loading screen*/
65
+
66
+ #loading-screen {
67
+ position: fixed;
68
+ top: 0;
69
+ left: 0;
70
+ width: 100%;
71
+ height: 100%;
72
+ background-image: url(images/background.png);
73
+ background-size: cover;
74
+ background-repeat: no-repeat;
75
+ display: flex;
76
+ justify-content: center;
77
+ align-items: center;
78
+ z-index: 9999;
79
+ }
80
+
81
+ .loading-spinner {
82
+ border: 8px solid lightgray;
83
+ border-top: 8px solid silver;
84
+ border-radius: 50%;
85
+ width: 50px;
86
+ height: 50px;
87
+ animation: spin 1s linear infinite;
88
+ }
89
+
90
+ @keyframes spin {
91
+ 0% {
92
+ transform: rotate(0deg);
93
+ }
94
+ 100% {
95
+ transform: rotate(360deg);
96
+ }
97
+ }
app/templates/index.html ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>Mobile Diagnostics</title>
7
+ <link rel="stylesheet" href="/static/myStyles.css" />
8
+ <script src="/static/script.js"></script>
9
+ <script src="https://cdn.botpress.cloud/webchat/v1/inject.js"></script>
10
+ <script src="https://mediafiles.botpress.cloud/9f157b03-6640-43c0-b364-fdf445bdcbcb/webchat/config.js" defer></script>
11
+ </head>
12
+
13
+ <body>
14
+ <div id="loading-screen">
15
+ <div class="loading-spinner"></div>
16
+ </div>
17
+
18
+ <div class="brand-container">
19
+ <img src="/static/images/brandName.png" alt="Mobile Diagnostics" width="1500px" />
20
+ <span>Your troubleshooting buddy on the go.</span>
21
+ </div>
22
+
23
+ <div class="buttons-container" id="buttons">
24
+ <label for="upload">
25
+ <img src="/static/images/detectPhone.png" alt="Upload Image" />
26
+ <input type="file" id="upload" accept=".png, .jpg, .jpeg" />
27
+ </label>
28
+
29
+ <label for="screenshot">
30
+ <img src="/static/images/errorScreenshot.png" alt="Screenshot Image" />
31
+ <input type="file" id="screenshot" accept=".png, .jpg, .jpeg" />
32
+ </label>
33
+
34
+ <label for="chat">
35
+ <img src="/static/images/chat.png" alt="Chat with Us" />
36
+ <button id="chat">Chat</button>
37
+ </label>
38
+
39
+ <div id="result"></div>
40
+ </div>
41
+
42
+ <script>
43
+ document.addEventListener("DOMContentLoaded", function () {
44
+ setTimeout(function () {
45
+ document.getElementById("loading-screen").style.display = "none";
46
+ }, 3000);
47
+ });
48
+
49
+ const upload = document.getElementById("upload");
50
+ const screenshot = document.getElementById("screenshot");
51
+ const chat = document.getElementById("chat");
52
+
53
+ function showBot() {
54
+ window.botpressWebChat.sendEvent({ type: "show" });
55
+ }
56
+
57
+ function hideButtons() {
58
+ document.getElementById("buttons").style.display = "none";
59
+ }
60
+
61
+ upload.addEventListener("change", function () {
62
+ var fileInput = document.getElementById("upload");
63
+
64
+ if (fileInput.files.length > 0) {
65
+ const file = fileInput.files[0];
66
+ const formData = new FormData();
67
+ formData.append("file", file);
68
+
69
+ fetch("/phone-recognition", {
70
+ method: "POST",
71
+ body: formData,
72
+ })
73
+ .then((response) => response.json())
74
+ .then((result) => {
75
+ const result_phone_model = result.result_phone_model;
76
+ console.log(result_phone_model);
77
+ window.botpressWebChat.sendPayload({
78
+ type: "text",
79
+ payload: {
80
+ result_phone_model,
81
+ pressedButton: "upload",
82
+ },
83
+ });
84
+
85
+ hideButtons();
86
+ showBot();
87
+ })
88
+ .catch((error) => {
89
+ console.error("Error during phone recognition:", error);
90
+ });
91
+ }
92
+ });
93
+
94
+ screenshot.addEventListener("change", function () {
95
+ var screenshotInput = document.getElementById("screenshot");
96
+
97
+ if (screenshotInput.files.length > 0) {
98
+ const file = screenshotInput.files[0];
99
+ const formData = new FormData();
100
+ formData.append("file", file);
101
+
102
+ fetch("/phone-recognition", {
103
+ method: "POST",
104
+ body: formData,
105
+ })
106
+ .then((response) => response.json())
107
+ .then((result) => {
108
+ const results_ocr = result.results_ocr;
109
+ console.log(results_ocr);
110
+ window.botpressWebChat.sendPayload({
111
+ type: "text",
112
+ payload: {
113
+ results_ocr,
114
+ pressedButton: "screenshot",
115
+ },
116
+ });
117
+
118
+ hideButtons();
119
+ showBot();
120
+ })
121
+ .catch((error) => {
122
+ console.error("Error during image processing:", error);
123
+ });
124
+ }
125
+ });
126
+
127
+ // Event listener for chat button
128
+ chat.addEventListener("click", function () {
129
+ window.botpressWebChat.sendPayload({
130
+ type: "text",
131
+ payload: {
132
+ pressedButton: "chat",
133
+ },
134
+ });
135
+ hideButtons();
136
+ showBot();
137
+ });
138
+ </script>
139
+ </body>
140
+ </html>