forked from theNewtonCode/EatEasy-smart-mess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_crop.py
99 lines (78 loc) · 3.07 KB
/
test_crop.py
1
2
3
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import cv2
import numpy as np
def crop_trapezium(image_path, trapezium_coords, output_dir):
# Load the image
img = cv2.imread(image_path)
# Define the trapezium coordinates in clockwise order
pts = np.array(trapezium_coords, np.int32)
# Create a mask with the same shape as the image
mask = np.zeros(img.shape[:2], np.uint8)
# Fill the trapezium with white color
cv2.fillPoly(mask, [pts], (255, 255, 255))
# Apply the mask to the image to get the cropped image
result = cv2.bitwise_and(img, img, mask=mask)
return result
# filename = os.path.splitext(os.path.basename(image_path))[0] + '_crop.jpg'
# output_path = os.path.join(output_dir, filename)
# # Save the cropped image to the specified output file
# cv2.imwrite(output_path, result)
# from PIL import Image
# # Load the image
# img = Image.open("hots.jpeg")
# # Get the size of the image
# width, height = img.size
# # Define the four corners of the image
# p = (0, 0)
# q = (width, 0)
# r = (0, height)
# s = (width, height)
# # Define the points on the sides pq and rs
# a = (int((1.4*width)/3), 0)
# b = (int((1.6*width)/3), 0)
# c = (int((1*width)/3), height)
# d = (int((2*width)/3), height)
# part1 = [p, a, c, r]
# part2 = [a, b, d, c]
# part3 = [b, q, s, d]
# # print(part1)
# crop_trapezium("pp8.jpeg", part1, "part1.jpg")
# crop_trapezium("pp8.jpeg", part2, "part2.jpg")
# crop_trapezium("pp8.jpeg", part3, "part3.jpg")
def take_nd_crop(listfimgs, img_name_count):
for f in listfimgs:
original_image_path = f
# Open the image using Pillow
image = Image.open(original_image_path)
# Define the dimensions of the cropped images
width, height = image.size
p = (0, 0)
q = (width, 0)
r = (0, height)
s = (width, height)
# Define the points on the sides pq and rs
a = (int((1.4*width)/3), 0)
b = (int((1.6*width)/3), 0)
c = (int((1*width)/3), height)
d = (int((2*width)/3), height)
part1 = [p, a, c, r]
part2 = [a, b, d, c]
part3 = [b, q, s, d]
coords = [part1, part2, part3]
# Loop through and crop the image into three equal sections
for i in range(3):
imgs = crop_trapezium(image, coords[i])
# Create a new filename for the cropped image
name = f"{f.split('.')[0]}_{i+1}"
filename = f"{name}.jpg"
# Define the path to the directory where the cropped images will be saved
save_directory = "broken_images"
# Save the cropped image to the specified directory
imgs.save(os.path.join(save_directory, filename))
# Count the number of people in the cropped image
crop_path = os.path.join(save_directory, filename)
num_people = count_people(crop_path)
img_name_count.get(name)[1] = num_people
# Print the number of people in the cropped image
# print(f"{filename} has {num_people} people.")
print(img_name_count)