<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import cv2
import pandas
from glob import glob
import math
from tqdm import tqdm
from scipy.signal import triang

train_img_paths = glob('sample/*.jpeg')
train_img_names = [img_path.split('/')[-1].split('.')[0] for img_path in train_img_paths]
train_data = zip(train_img_paths, train_img_names)
df = pandas.read_csv('trainLabels.csv')
data_dict = {}
for train_img_path ,train_img_name in train_data:
  row = df.loc[df['image'] == train_img_name]
  label = row.iloc[0]['level']
  data_dict[train_img_path] = label

print(data_dict)
def separation_folder_hough(paths, out_dir):
  for path in paths:
    base_name = path.split('/')[1]
    img = cv2.imread(path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 10, 10, minRadius = 900, maxRadius = 1700)
    circle  = circles[0][0]
    circle[2] *= 1.05
    
    min_x = np.clip(int(circle[0]-circle[2]), 0, img.shape[1]-1)
    max_x = np.clip(int(circle[0]+circle[2]), 0, img.shape[1]-1)
    min_y = np.clip(int(circle[1]-circle[2]), 0, img.shape[0]-1)
    max_y = np.clip(int(circle[1]+circle[2]), 0, img.shape[0]-1)
    
    img = img[min_y:max_y, min_x:max_x, ...]
    cv2.imwrite(out_dir+'/'+base_name, img)  

def resize_folder(paths, out_dir, out_res):
  for path in paths:
    base_name = path.split('/')[1]
    img  = cv2.imread(path)
    if img.shape[0]>img.shape[1]:
      dx = int(out_res/img.shape[0] * img.shape[1])
      img = cv2.resize(img, (dx, out_res), interpolation = cv2.INTER_LINEAR)
    else:
      dy = int(out_res/img.shape[1] * img.shape[0])
      img = cv2.resize(img, (out_res, dy), interpolation = cv2.INTER_LINEAR)
      sy = (out_res-img.shape[0])//2
      sx = (out_res-img.shape[1])//2
      out_img = np.zeros((out_res, out_res, 3))
      out_img[sy:img.shape[0]+sy, sx:img.shape[1]+sx, ...] = img
      cv2.imwrite(out_dir+'/'+base_name, out_img)
separation_folder_hough(train_img_paths, 'sample')
resize_folder(train_img_paths, 'sample', 1024)
# TODO - studenti pouziji houghovu transformaci z githubu od Petra na oriznuti
# res = np.zeros((2), dtype=np.float32)
# for img_path in tqdm(data_dict.keys()):
#  img = cv2.imread(img_path)
#  res += img.shape[:2]
# avg_res = (res / len(data_dict.keys())).astype(np.int32)
# print(avg_res)

avg_res = [1024, 1024]
factor = 3
len(data_dict.keys()) * (avg_res[0] / factor) * (avg_res[1] / factor) * 1 * 32 / 8 / 1024 / 1024 / 1024
train_x = np.zeros((len(data_dict.keys()), math.ceil(avg_res[0] / factor) * math.ceil(avg_res[1] / factor)), dtype=np.float32)
train_y = np.zeros(len(data_dict.keys()), dtype=np.int32)
# vyzkouset jiny konvolucni kernel
tr = triang(factor*2+1).reshape(factor*2+1, 1)
kernel = np.dot(tr, tr.T)
kernel /= np.sum(kernel)
i = 0
for img_path in tqdm(data_dict.keys()):
  img = cv2.imread(img_path)
  img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  img = cv2.resize(img, (avg_res[1], avg_res[0]))
  img = cv2.filter2D(img, -1, kernel)[::factor, ::factor, ...]
  # feature extraction, student muze vlozit vlastni vektor features
  train_x[i, ...] = img.flatten() / 255.0
  train_y[i] = data_dict[img_path]
  i += 1

train_y[train_y != 0] = -1
train_y[train_y == 0] = 1
# https://docs.opencv.org/3.4/d1/d73/tutorial_introduction_to_svm.html
svm = cv2.ml.SVM_create()
svm.setType(cv2.ml.SVM_C_SVC)
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setTermCriteria((cv2.TERM_CRITERIA_MAX_ITER, 100, 1e-6))
svm.train(train_x, cv2.ml.ROW_SAMPLE, train_y)
# predikovat ne trenovaci data, ale testovaci
predictions = np.copy(train_y)
for i in tqdm(range(train_x.shape[0])):
  predictions[i] = svm.predict(train_x[i:i+1, ...])[1]

ind = predictions == train_y
print()
print(np.sum(ind), np.sum(ind) / train_y.shape[0] * 100)<jupyter_output>100%|██████████| 10/10 [00:00<00:00, 1564.28it/s]
{'fraction_non_alphanumeric': 0.17274815003827507, 'fraction_numerical': 0.046185251339627455, 'mean_word_length': 3.930817610062893, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '156478', 'license': 'MIT', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1672, 'n_words': 290, 'path': 'P20038/svm.ipynb', 'repo_name': 'Mastemace/GZAPV_2021', 'size': '3919'}