Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,66 +1,59 @@
|
|
1 |
-
import cv2
|
2 |
-
import numpy as np
|
3 |
-
import matplotlib.pyplot as plt
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
plt.subplot(
|
46 |
-
plt.imshow(
|
47 |
-
plt.title('
|
48 |
-
|
49 |
-
plt.
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
# Calculate redness score and display the results
|
61 |
-
redness_score, red_mask = calculate_redness_score(image_path)
|
62 |
-
print(f'Redness Score: {redness_score:.2f}')
|
63 |
-
|
64 |
-
|
65 |
-
if red_mask is not None:
|
66 |
-
cv2.imwrite('red_mask.jpg', red_mask)
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from sklearn.ensemble import RandomForestRegressor
|
5 |
+
from skimage import filters
|
6 |
+
|
7 |
+
def calculate_redness_score(image_path):
|
8 |
+
img = cv2.imread(image_path)
|
9 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
10 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
11 |
+
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
12 |
+
faces = face_cascade.detectMultiScale(gray_img, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
13 |
+
|
14 |
+
if len(faces) == 0:
|
15 |
+
return 0, None
|
16 |
+
|
17 |
+
x, y, w, h = faces[0]
|
18 |
+
face_roi = img[y:y+h, x:x+w]
|
19 |
+
face_hsv = cv2.cvtColor(face_roi, cv2.COLOR_BGR2HSV)
|
20 |
+
lower_red = np.array([0, 70, 100])
|
21 |
+
upper_red = np.array([10, 255, 255])
|
22 |
+
mask = cv2.inRange(face_hsv, lower_red, upper_red)
|
23 |
+
redness_percentage = (np.count_nonzero(mask) / (w * h)) * 100
|
24 |
+
|
25 |
+
edge_mask = filters.sobel(mask)
|
26 |
+
segmented_image = cv2.watershed(face_roi, edge_mask)
|
27 |
+
|
28 |
+
regressor = RandomForestRegressor()
|
29 |
+
X = np.array([[x, y, w, h] for x, y, w, h in faces])
|
30 |
+
y = np.array([redness_percentage for _ in range(len(faces))])
|
31 |
+
regressor.fit(X, y)
|
32 |
+
|
33 |
+
plt.subplot(2, 2, 1)
|
34 |
+
plt.imshow(img_rgb)
|
35 |
+
plt.title('Original Image')
|
36 |
+
|
37 |
+
plt.subplot(2, 2, 2)
|
38 |
+
plt.imshow(cv2.cvtColor(cv2.bitwise_and(face_roi, face_roi, mask=mask), cv2.COLOR_BGR2RGB))
|
39 |
+
plt.title('Red Mask')
|
40 |
+
|
41 |
+
plt.subplot(2, 2, 3)
|
42 |
+
plt.imshow(edge_mask, cmap='gray')
|
43 |
+
plt.title('Edge Mask')
|
44 |
+
|
45 |
+
plt.subplot(2, 2, 4)
|
46 |
+
plt.imshow(segmented_image)
|
47 |
+
plt.title('Segmented Image')
|
48 |
+
|
49 |
+
plt.show()
|
50 |
+
|
51 |
+
return redness_percentage, mask
|
52 |
+
|
53 |
+
image_path = 'main-qimg-8f68ebd077d3e8c95f1af2a120adbe90-lq.jpg'
|
54 |
+
|
55 |
+
redness_score, red_mask = calculate_redness_score(image_path)
|
56 |
+
print(f'Redness Score: {redness_score:.2f}')
|
57 |
+
|
58 |
+
if red_mask is not None:
|
59 |
+
cv2.imwrite('red_mask.jpg', red_mask)
|
|
|
|
|
|
|
|
|
|
|
|
|
|