-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2.py
More file actions
69 lines (56 loc) · 1.71 KB
/
Copy pathtask2.py
File metadata and controls
69 lines (56 loc) · 1.71 KB
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
import numpy as np
import os
import cv2 as cv
folder="test_case_2"
def empty_row(img, i):
for j in range(len(img[0])):
if img[i][j] != 0:
return False
return True
def count_rows(img1):
img1 = img1[25:1800, :]
img1 = cv.cvtColor(img1, cv.COLOR_BGR2GRAY)
img1 = cv.Canny(img1, 150, 255)
row_count = 0
for i in range(len(img1) - 1):
if empty_row(img1, i) and not empty_row(img1, i + 1):
row_count += 1
return row_count - 1
def checkAnswer(width, pos):
if pos < width / 4:
return "A"
elif pos < width / 2:
return "B"
elif pos < 3 * width / 4:
return "C"
elif pos < width:
return "D"
else:
return None
def find_sol(img, row_num):
img = img[200:1800, 274:1414]
img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
question_list = np.split(img, row_num)
cnt = 1
sol = dict()
cv.imwrite("final_image.jpg", img)
for i in question_list:
t, i = cv.threshold(i, 30, 255, cv.THRESH_BINARY)
i = cv.GaussianBlur(i, (125, 125), 0)
width = i.shape[1]
loc = cv.minMaxLoc(i)
white_width = loc[3][0]
ans = checkAnswer(width, white_width)
sol[cnt] = ans
cnt += 1
return sol
dir_list = os.listdir(folder)
img = cv.imread(folder + "/" + dir_list[0])
for i in range(1, len(dir_list)):
if dir_list[i].endswith('.jpg') or dir_list[i].endswith('.png'):
img1 = cv.imread(folder + "/" + dir_list[i])
if img1.shape != img.shape:
img1 = cv.resize(img1, (img.shape[1], img.shape[0]))
img = cv.bitwise_or(img, img1)
Solution_Found = find_sol(img, count_rows(img))
print("Answers:", Solution_Found)