fix README and move repo from Codeberg to HF
Browse files- README.md +1 -2
- preprocess.cpp +240 -0
- preprocess.h +36 -0
README.md
CHANGED
|
@@ -7,5 +7,4 @@ pipeline_tag: image-classification
|
|
| 7 |
license: gpl-3.0
|
| 8 |
---
|
| 9 |
|
| 10 |
-
### This repo will host all iterations of models from our
|
| 11 |
-
[Associated Codeberg repo](https://codeberg.org/Simtoon.Interactive.LLC/neko_classifier)
|
|
|
|
| 7 |
license: gpl-3.0
|
| 8 |
---
|
| 9 |
|
| 10 |
+
### This repo will host all iterations of models from our neko-classifier project.
|
|
|
preprocess.cpp
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include "preprocess.h"
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
void preprocessor::loadImg()
|
| 5 |
+
{
|
| 6 |
+
std::cout << "imread neko img\n";
|
| 7 |
+
for(auto& p : std::filesystem::directory_iterator("/home/simtoon/datasets/unprocessed/neko"))
|
| 8 |
+
{
|
| 9 |
+
cv::Mat img = cv::imread(p.path().string());
|
| 10 |
+
if(!img.empty())
|
| 11 |
+
{
|
| 12 |
+
preprocessor::animeNekoGirls.push_back(img);
|
| 13 |
+
}
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
std::cout << "imread non_neko img\n";
|
| 17 |
+
for(auto& p : std::filesystem::directory_iterator("/home/simtoon/datasets/unprocessed/non_neko"))
|
| 18 |
+
{
|
| 19 |
+
cv::Mat img = cv::imread(p.path().string());
|
| 20 |
+
if(!img.empty())
|
| 21 |
+
{
|
| 22 |
+
preprocessor::nonAnimeNekoGirls.push_back(img);
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
void preprocessor::resizeImg(int size)
|
| 28 |
+
{
|
| 29 |
+
std::cout << "resizing neko img\n";
|
| 30 |
+
cv::Size sizeObj(size, size); //need to construct an obj of class cv::Size with the int size arg for both width and height
|
| 31 |
+
for(auto& img : preprocessor::animeNekoGirls)
|
| 32 |
+
{
|
| 33 |
+
cv::resize(img, img, sizeObj);
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
std::cout << "resizing non_neko img\n";
|
| 37 |
+
for(auto& img : preprocessor::nonAnimeNekoGirls)
|
| 38 |
+
{
|
| 39 |
+
cv::resize(img, img, sizeObj);
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
void preprocessor::normalizeImg()
|
| 44 |
+
{
|
| 45 |
+
std::cout << "normalizing neko img\n";
|
| 46 |
+
for(auto& img : preprocessor::animeNekoGirls)
|
| 47 |
+
{
|
| 48 |
+
img /= 255.0;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
std::cout << "normalizing non_neko img\n";
|
| 52 |
+
for(auto& img : preprocessor::nonAnimeNekoGirls)
|
| 53 |
+
{
|
| 54 |
+
img /= 255.0;
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
void preprocessor::convertToGrayscaleImg()
|
| 59 |
+
{
|
| 60 |
+
std::cout << "converting to grayscale neko img\n";
|
| 61 |
+
for(auto& img : preprocessor::animeNekoGirls)
|
| 62 |
+
{
|
| 63 |
+
cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
std::cout << "converting to grayscale non_neko img\n";
|
| 67 |
+
for(auto& img : preprocessor::nonAnimeNekoGirls)
|
| 68 |
+
{
|
| 69 |
+
cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
void preprocessor::blurImg(int blurType, int kerx, int kery)
|
| 74 |
+
{
|
| 75 |
+
if(blurType == 0)
|
| 76 |
+
{
|
| 77 |
+
std::cout << "blurring neko img\n";
|
| 78 |
+
for(auto& img : preprocessor::animeNekoGirls)
|
| 79 |
+
{
|
| 80 |
+
cv::GaussianBlur(img, img, cv::Size(kerx, kery), 0);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
std::cout << "blurring non_neko img\n";
|
| 84 |
+
for(auto& img : preprocessor::nonAnimeNekoGirls)
|
| 85 |
+
{
|
| 86 |
+
cv::GaussianBlur(img, img, cv::Size(kerx, kery), 0);
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
void preprocessor::detectEdgesImg(int algType, int thr1, int thr2)
|
| 92 |
+
{
|
| 93 |
+
if(algType == 0)
|
| 94 |
+
{
|
| 95 |
+
std::cout << "applying canny to neko img\n";
|
| 96 |
+
for(auto& img : preprocessor::animeNekoGirls)
|
| 97 |
+
{
|
| 98 |
+
cv::Canny(img, img, thr1, thr2);
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
std::cout << "applying canny to non_neko img\n";
|
| 102 |
+
for(auto& img : preprocessor::nonAnimeNekoGirls)
|
| 103 |
+
{
|
| 104 |
+
cv::Canny(img, img, thr1, thr2);
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
void preprocessor::detectContours()
|
| 110 |
+
{
|
| 111 |
+
std::vector<std::vector<cv::Point>> conts;
|
| 112 |
+
|
| 113 |
+
std::cout << "applying thresholding and finding contours in neko img\n";
|
| 114 |
+
for(auto& img : preprocessor::animeNekoGirls)
|
| 115 |
+
{
|
| 116 |
+
cv::threshold(img, img, 125, 255, cv::THRESH_BINARY);
|
| 117 |
+
cv::findContours(img, conts, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
|
| 118 |
+
for(size_t i = 0; i < conts.size(); i++)
|
| 119 |
+
{
|
| 120 |
+
cv::drawContours(img, {conts}, i, cv::Scalar(255,0,0), 2);
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
std::cout << "applying thresholding and finding contours in non-neko img\n";
|
| 125 |
+
for(auto& img : preprocessor::nonAnimeNekoGirls)
|
| 126 |
+
{
|
| 127 |
+
cv::threshold(img, img, 80, 255, cv::THRESH_BINARY);
|
| 128 |
+
cv::findContours(img, conts, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
|
| 129 |
+
for(size_t i = 0; i < conts.size(); i++)
|
| 130 |
+
{
|
| 131 |
+
cv::drawContours(img, {conts}, i, cv::Scalar(255,0,0), 2);
|
| 132 |
+
}
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
void preprocessor::harrisImg()
|
| 137 |
+
{
|
| 138 |
+
std::cout << "applying harris to neko img\n";
|
| 139 |
+
for(auto& img : preprocessor::animeNekoGirls)
|
| 140 |
+
{
|
| 141 |
+
img.convertTo(img, CV_32F);
|
| 142 |
+
cv::cornerHarris(img, img, 2, 1, 0.1);
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
std::cout << "applying harris to non_neko img\n";
|
| 146 |
+
for(auto& img : preprocessor::nonAnimeNekoGirls)
|
| 147 |
+
{
|
| 148 |
+
img.convertTo(img, CV_32F);
|
| 149 |
+
cv::cornerHarris(img, img, 2, 5, 0.03);
|
| 150 |
+
}
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
void preprocessor::SIFTimg()
|
| 154 |
+
{
|
| 155 |
+
cv::Ptr<cv::SIFT> SIFTdetector = cv::SIFT::create();
|
| 156 |
+
|
| 157 |
+
for(auto& img : preprocessor::animeNekoGirls)
|
| 158 |
+
{
|
| 159 |
+
std::vector<cv::KeyPoint> kpoints;
|
| 160 |
+
SIFTdetector->detect(img, kpoints);
|
| 161 |
+
cv::Mat img_kpoints;
|
| 162 |
+
}
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
bool preprocessor::boolDetectHuman()
|
| 166 |
+
{
|
| 167 |
+
cv::HOGDescriptor hog;
|
| 168 |
+
hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());
|
| 169 |
+
cv::Mat img = cv::imread("/home/simtoon/img.jpg");
|
| 170 |
+
|
| 171 |
+
std::vector<cv::Rect> ppl;
|
| 172 |
+
hog.detectMultiScale(img, ppl);
|
| 173 |
+
|
| 174 |
+
for (cv::Rect rect : ppl)
|
| 175 |
+
{
|
| 176 |
+
cv::rectangle(img, rect, cv::Scalar(0, 0, 255), 2);
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
if(ppl.empty())
|
| 180 |
+
{
|
| 181 |
+
return false;
|
| 182 |
+
}
|
| 183 |
+
else
|
| 184 |
+
{
|
| 185 |
+
return true;
|
| 186 |
+
cv::imshow("humanzz", img);
|
| 187 |
+
cv::waitKey();
|
| 188 |
+
}
|
| 189 |
+
return false;
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
void preprocessor::imwriteToDiskImg()
|
| 193 |
+
{
|
| 194 |
+
std::cout << "writing pre-processed neko img\n";
|
| 195 |
+
for(size_t i = 0; i < preprocessor::animeNekoGirls.size(); i++)
|
| 196 |
+
{
|
| 197 |
+
//preprocessor::animeNekoGirls[i].convertTo(preprocessor::animeNekoGirls[i], CV_16UC3, 255,255);
|
| 198 |
+
cv::imwrite("/home/simtoon/datasets/processed/neko/" + std::to_string(i) + ".jpeg", preprocessor::animeNekoGirls[i] /** 255*/);
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
std::cout << "writing pre-processed non_neko img\n";
|
| 202 |
+
for(size_t i = 0; i < preprocessor::nonAnimeNekoGirls.size(); i++)
|
| 203 |
+
{
|
| 204 |
+
//preprocessor::nonAnimeNekoGirls[i].convertTo(preprocessor::nonAnimeNekoGirls[i], CV_16UC3, 255,255);
|
| 205 |
+
cv::imwrite("/home/simtoon/datasets/processed/non_neko/" + std::to_string(i) + ".jpeg", preprocessor::nonAnimeNekoGirls[i] /** 255*/);
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
int main()
|
| 211 |
+
{
|
| 212 |
+
preprocessor Preprocessor;
|
| 213 |
+
|
| 214 |
+
Preprocessor.boolDetectHuman();
|
| 215 |
+
|
| 216 |
+
Preprocessor.loadImg();
|
| 217 |
+
Preprocessor.resizeImg();
|
| 218 |
+
//Preprocessor.normalizeImg(); fucked up, not calling until a fix is found
|
| 219 |
+
std::cout << "Press any key to proceed to the next step";
|
| 220 |
+
std::cin.ignore();
|
| 221 |
+
Preprocessor.convertToGrayscaleImg();
|
| 222 |
+
Preprocessor.imwriteToDiskImg();
|
| 223 |
+
std::cout << "Press any key to proceed to the next step";
|
| 224 |
+
std::cin.ignore();
|
| 225 |
+
Preprocessor.blurImg(0, 5, 5);
|
| 226 |
+
Preprocessor.imwriteToDiskImg();
|
| 227 |
+
std::cout << "Press any key to proceed to the next step";
|
| 228 |
+
std::cin.ignore();
|
| 229 |
+
//Preprocessor.detectContours();
|
| 230 |
+
//Preprocessor.imwriteToDiskImg();
|
| 231 |
+
//std::cout << "Press any key to proceed to the next step";
|
| 232 |
+
//std::cin.ignore();
|
| 233 |
+
//Preprocessor.detectEdgesImg(0, 200, 350);
|
| 234 |
+
// Preprocessor.harrisImg();
|
| 235 |
+
// Preprocessor.imwriteToDiskImg();
|
| 236 |
+
// std::cout << "Press any key to proceed to the next step";
|
| 237 |
+
// std::cin.ignore();
|
| 238 |
+
//Preprocessor.detectEdgesImg(0, 200, 300);
|
| 239 |
+
//Preprocessor.imwriteToDiskImg();
|
| 240 |
+
}
|
preprocess.h
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <opencv2/core/hal/interface.h>
|
| 4 |
+
#include <opencv2/core/mat.hpp>
|
| 5 |
+
#include <opencv2/core/types.hpp>
|
| 6 |
+
#include <opencv2/imgcodecs.hpp>
|
| 7 |
+
#include <opencv2/imgproc.hpp>
|
| 8 |
+
#include <opencv2/features2d.hpp>
|
| 9 |
+
#include <opencv2/opencv.hpp>
|
| 10 |
+
#include <opencv2/highgui.hpp>
|
| 11 |
+
#include <opencv2/objdetect.hpp>
|
| 12 |
+
#include <cstddef>
|
| 13 |
+
#include <filesystem>
|
| 14 |
+
#include <iostream>
|
| 15 |
+
#include <string>
|
| 16 |
+
#include <vector>
|
| 17 |
+
|
| 18 |
+
class preprocessor
|
| 19 |
+
{
|
| 20 |
+
public:
|
| 21 |
+
// vars
|
| 22 |
+
std::vector<cv::Mat> animeNekoGirls;
|
| 23 |
+
std::vector<cv::Mat> nonAnimeNekoGirls;
|
| 24 |
+
// methods
|
| 25 |
+
void loadImg();
|
| 26 |
+
void resizeImg(int size=256); //to 256x256 atm
|
| 27 |
+
void normalizeImg(); //not doing norm anymore since the result is fucked up; no idea how to fix atm
|
| 28 |
+
void convertToGrayscaleImg(); //'cause no colors equals less computational resources required
|
| 29 |
+
void blurImg(int blurType, int kerx, int kery); //0 for gaussian, 1 for ...
|
| 30 |
+
void detectEdgesImg(int algType, int thr1, int thr2); //0 for canny, 1 for ...
|
| 31 |
+
void detectContours();
|
| 32 |
+
void harrisImg(); //apply the Harris detection algo to our two classes
|
| 33 |
+
void SIFTimg(); //I don't think this one will help us much
|
| 34 |
+
bool boolDetectHuman();
|
| 35 |
+
void imwriteToDiskImg();
|
| 36 |
+
};
|