-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
110 lines (100 loc) · 4.04 KB
/
test.py
File metadata and controls
110 lines (100 loc) · 4.04 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
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
100
101
102
103
104
105
106
107
108
109
110
from PIL import Image
import numpy as np
import os
from tqdm import tqdm
import time
import json
import sys
def ResizeImage(image_path, size):
try:
with Image.open(image_path) as img:
if img.mode != "RGB":
img = img.convert("RGB")
resized_img = img.resize(size)
resized_img.save(image_path, format="JPEG", quality=95)
except Exception as e:
print(f"An error occurred: {e}")
return
def GenerateImageVectors(image_path, row, column, bins=256):
img = Image.open(image_path).convert("L")
width, height = img.size
step_x = width // column
step_y = height // row
boxes = [
(i * step_x, j * step_y, (i + 1) * step_x, (j + 1) * step_y)
for j in range(row) for i in range(col)
]
histograms = []
for box in boxes:
cropped_img = np.array(img.crop(box))
histogram = np.zeros(bins)
for element in cropped_img.flatten():
if element >= 0 and element < bins :
histogram[element] += 1
histograms.append(histogram)
return histograms
def distance(histogram1, histogram2) :
return np.sqrt(np.sum((histogram1 - histogram2) ** 2))
def CheckImage(image_path, row, col, positive, negative, bins=256) :
ResizeImage(image_path, (224, 224))
histograms= GenerateImageVectors(image_path, row, col, bins)
diff1 = [ 0 for _ in range(row * col) ]
diff2 = [ 0 for _ in range(row * col) ]
for i in range(row * col):
diff1[i] = distance(histograms[i], positive[i])
diff2[i] = distance(histograms[i], negative[i])
count_1 = 0
count_2 = 0
for i in range (row * col) :
if diff1[i] < diff2[i] :
count_1 += 1
elif diff2[i] < diff1[i] :
count_2 += 1
if count_1 > count_2 :
return 1, (count_1 * 100) / (row * col)
elif count_2 > count_1 :
return 0, (count_2 * 100) / (row * col)
else :
return 2, 50
def GetDataFromJsonFile(filename) :
with open(filename, 'r') as jsonfile:
return json.load(jsonfile)
def GetAccuracy(directory_path, row, col, positive, negative, bins=256):
accuracy = 0;
count = 0
path = directory_path + '\\positive'
for file_name in os.listdir(path):
file_path = os.path.join(path, file_name)
if os.path.isfile(file_path):
count += 1
result, prcg = CheckImage(file_path, row, col, positive, negative, bins)
if result == 1:
accuracy += 1
print(f"{file_name} => [ positive: {prcg}%, negative : {100 - prcg}% ]")
elif result == 0:
print(f"{file_name} => [ positive: {100 - prcg}%, negative : {prcg}% ]")
else :
print(f"{file_name} => [ positive: {100 - prcg}%, negative : {prcg}% ]")
path = directory_path + '\\negative'
for file_name in os.listdir(path):
file_path = os.path.join(path, file_name)
if os.path.isfile(file_path):
count += 1
result, prcg = CheckImage(file_path, row, col, positive, negative, bins)
if result == 1:
print(f"{file_name} => [ positive: {prcg}%, negative : {100 - prcg}% ]")
elif result == 0:
accuracy += 1
print(f"{file_name} => [ positive: {100 - prcg}%, negative : {prcg}% ]")
else :
print(f"{file_name} => [ positive: {100 - prcg}%, negative : {prcg}% ]")
return (accuracy * 100) / count
print("Brain tumor detection using AI (testing) : ")
row = int(input("enter the number of rows : "))
col = int(input("enter the number of columns : "))
bins = int(input("enter the number of bins : "))
folder_path = input("enter the testing folder path : ")
histograms = GetDataFromJsonFile('trained.json')
positive = histograms['positive']
negative = histograms['negative']
print("accuracy : ", GetAccuracy(folder_path, row, col, positive, negative, bins), "%")