AmirKaseb commited on
Commit
87b6396
·
verified ·
1 Parent(s): d263491

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +59 -66
main.py CHANGED
@@ -1,66 +1,59 @@
1
- import cv2
2
- import numpy as np
3
- import matplotlib.pyplot as plt
4
-
5
- def calculate_redness_score(image_path):
6
- # Load the image
7
- img = cv2.imread(image_path)
8
-
9
- # Convert BGR image to RGB
10
- img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
11
-
12
- # Load the pre-trained face detector from OpenCV
13
- face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
14
-
15
- # Convert image to grayscale for face detection
16
- gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
17
-
18
- # Detect faces in the image
19
- faces = face_cascade.detectMultiScale(gray_img, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
20
-
21
- # If no faces are detected, return 0
22
- if len(faces) == 0:
23
- return 0, None
24
-
25
- # Get the first detected face
26
- x, y, w, h = faces[0]
27
-
28
- # Extract the region of interest (ROI) which is the face
29
- face_roi = img[y:y+h, x:x+w]
30
-
31
- # Convert the ROI to HSV color space
32
- face_hsv = cv2.cvtColor(face_roi, cv2.COLOR_BGR2HSV)
33
-
34
- # Define a range for red color in HSV
35
- lower_red = np.array([0, 70, 100])
36
- upper_red = np.array([10, 255, 255])
37
-
38
- # Create a mask for red color in the ROI
39
- mask = cv2.inRange(face_hsv, lower_red, upper_red)
40
-
41
- # Calculate the percentage of redness in the face
42
- redness_percentage = (np.count_nonzero(mask) / (w * h))+ 100 - 30
43
-
44
- # Display the original image with the detected face and red mask
45
- plt.subplot(1, 2, 1)
46
- plt.imshow(img_rgb)
47
- plt.title('Original Image')
48
-
49
- plt.subplot(1, 2, 2)
50
- plt.imshow(cv2.cvtColor(cv2.bitwise_and(face_roi, face_roi, mask=mask), cv2.COLOR_BGR2RGB))
51
- plt.title('Red Mask')
52
-
53
- plt.show()
54
-
55
- return redness_percentage, mask
56
-
57
- # Specify the image path
58
- image_path = 'main-qimg-8f68ebd077d3e8c95f1af2a120adbe90-lq.jpg'
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)