-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
89 lines (66 loc) · 2.41 KB
/
main.py
File metadata and controls
89 lines (66 loc) · 2.41 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
import pandas as pd
import cv2
import matplotlib.pyplot as plt
from typing import List
import numpy as np
def img_height(path: str) -> int:
img = cv2.imread(path)
return img.shape[0]
def img_width(path: str) -> int:
img = cv2.imread(path)
return img.shape[1]
def img_channels(path: str) -> int:
img = cv2.imread(path)
return img.shape[2]
def img_pixels(path: str) -> int:
img = cv2.imread(path)
return img.size
def create_DataFrame() -> pd.DataFrame:
df1 = pd.read_csv('Brown bear annotation', header=None)
df2 = pd.read_csv('Polar bear annotation', header=None)
df3 = pd.concat([df1, df2], ignore_index=None)
df3.drop(1, axis=1, inplace=True)
df3.rename(columns={0: 'Path', 2: 'ClassName'}, inplace=True)
data = []
for i in df3['ClassName']:
if i == 'brown bear':
data.append(0)
elif i == 'polar bear':
data.append(1)
df3['mark'] = data
df3['height'] = df3['Path'].apply(img_height)
df3['width'] = df3['Path'].apply(img_width)
df3['channels'] = df3['Path'].apply(img_channels)
return df3
def df_mark_filter(df: pd.DataFrame, class_mark: int) -> pd.DataFrame:
return df[df['mark'] == class_mark]
def df_dimentions_filter(df: pd.DataFrame, m_Height: int, m_Weight: int, class_mark: int) -> pd.DataFrame:
return df[(df.mark == class_mark) & (df.height <= m_Height) & (df.width <= m_Weight)]
def df_pixel_statistics(df: pd.DataFrame, class_mark: int) -> pd.DataFrame:
df['pixel'] = df['Path'].apply(img_pixels)
df = df_mark_filter(df, class_mark)
df.groupby('pixel').count()
print(df.pixel.describe())
def create_histogram(df: pd.DataFrame, class_mark: int) -> List[np.ndarray]:
df = df_mark_filter(df, class_mark)
df = df.sample()
for item in df['Path']:
path = item
img = cv2.imread(path)
color = ('b', 'g', 'r')
result = [[], []]
for i, col in enumerate(color):
histr = cv2.calcHist([img], [i], None, [256], [0, 256])
result[0].append(histr)
result[1].append(col)
return result
def draw_histrogram(df: pd.DataFrame, class_mark: int) -> None:
tmp = create_histogram(df, class_mark)
for i in range(len(tmp[0])):
plt.plot(tmp[0][i], color=tmp[1][i])
plt.xlim([0, 256])
plt.xlabel("Intensity")
plt.ylabel("Number of pixels")
plt.show()
if __name__ == '__main__':
df = create_DataFrame()