Skip to content

Instantly share code, notes, and snippets.

@py7hon
Last active July 31, 2020 11:06
Show Gist options
  • Save py7hon/7340555642caf55d70370f6fa07749bd to your computer and use it in GitHub Desktop.
Save py7hon/7340555642caf55d70370f6fa07749bd to your computer and use it in GitHub Desktop.
Mencari Doujin dan Manga Dengan Potongan Gambar
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "cari.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "lqq7McHGVEG_",
"colab_type": "text"
},
"source": [
"# Mencari Doujin atau Manga dengan Gambar"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3alvY3HpVi9v",
"colab_type": "text"
},
"source": [
"##### Ingat ini hanya untuk pembelajaran\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_wfBb8c9WDcT",
"colab_type": "text"
},
"source": [
"## 1. Mempersiapkan Dependencies "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tc0LOUheWSbj",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"* numpy\n",
"* opencv-python\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cqhOQ7OVWk9I",
"colab_type": "text"
},
"source": [
"## 2. Install Dependencies"
]
},
{
"cell_type": "code",
"metadata": {
"id": "fB-ExiCXW_BD",
"colab_type": "code",
"colab": {}
},
"source": [
"pip install numpy opencv-python"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "UYAE21PKXBPM",
"colab_type": "text"
},
"source": [
"## 3. Silahkan copy Folder FAKKU"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "afoH806lXUwk",
"colab_type": "text"
},
"source": [
"https://drive.google.com/drive/folders/1-_JkK-AE6JBlcp9vR-eGlyBPdITo1-yh?usp=sharing\n",
"\n",
"*jika memerlukan ijin silahkan PM [Iqbal Rifai](https://m.me/composer.json)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "W1q5pJY8Y3wk",
"colab_type": "text"
},
"source": [
"## 4. Pindah ke Folder"
]
},
{
"cell_type": "code",
"metadata": {
"id": "H7jcLr64XUFn",
"colab_type": "code",
"colab": {}
},
"source": [
"cd FAKKU"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "iyVGfQ7NZKL4",
"colab_type": "text"
},
"source": [
"## 5. Upload Gambar yang ingin di cari ke Folder FAKKU"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "biOnNtjTZk1Y",
"colab_type": "text"
},
"source": [
"## 6. Eksekusi dengan menjalankan Script di bawah ini"
]
},
{
"cell_type": "code",
"metadata": {
"id": "mJTbqAoUZwYV",
"colab_type": "code",
"colab": {}
},
"source": [
"import sys, os\n",
"import argparse\n",
"import numpy as np\n",
"import zipfile\n",
"import cv2\n",
"import datetime\n",
"\n",
"#Import Image to be searched for\n",
"#parser = argparse.ArgumentParser()\n",
"#parser.add_argument('-i', type=argparse.FileType('r', encoding='UTF-8'), required=True)\n",
"#args = parser.parse_args()\n",
"#small_image = cv2.imread(args)\n",
"\n",
"\n",
"startimage = \"image.png\" #Ganti dengan nama gambar yang tadi di Upload\n",
"small_image = cv2.imread(startimage)\n",
"\n",
"#create vars\n",
"filelist = []\n",
"similarity = []\n",
"bestMatch = sys.float_info.max\n",
"counter = -1\n",
"\n",
"##get all zips in subdirectories\n",
"for subdir, dirs, files in os.walk(\".\"):\n",
" for dir in dirs:\n",
" path = os.path.join(subdir, dir)\n",
" for file in os.listdir(path):\n",
" if file.endswith(\".zip\"): \n",
" filelist.append(os.path.join(path, file))\n",
"\n",
"#go through all found zips\n",
"for file in filelist:\n",
" counter += 1\n",
" print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + \": progress: \" \n",
" + str(counter) + \"/\" + str(len(filelist)))\n",
" zippedImgs = zipfile.ZipFile(file)\n",
"\n",
" if(bestMatch == 0.0):\n",
" break\n",
"\n",
" #go through all images in all zips\n",
" for img in zippedImgs.namelist():\n",
" if(\".jpg\" in img or \".png\" in img or \".gif\" in img):\n",
" #transform zipped files to images\n",
" data = zippedImgs.read(img)\n",
" nparr = np.fromstring(data, np.uint8)\n",
" large_image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)\n",
"\n",
" #check if image is a closer match than all previous images\n",
" if(large_image.shape[0] < small_image.shape[0] or large_image.shape[1] < small_image.shape[1]):\n",
" continue\n",
"\n",
" resultData = cv2.matchTemplate(large_image, small_image, cv2.TM_SQDIFF_NORMED)\n",
" result = cv2.minMaxLoc(resultData);\n",
" similarity.append([result, file, img])\n",
"\n",
" #Draw result rectangle\n",
" if(result[0] < bestMatch):\n",
" bestMatch = result[0]\n",
" # We want the minimum squared difference\n",
" mn,_,mnLoc,_ = cv2.minMaxLoc(resultData)\n",
" # Draw the rectangle:\n",
" # Step 1: Extract the coordinates of our best match\n",
" MPx,MPy = mnLoc\n",
" # Step 2: Get the size of the template. This is the same size as the match.\n",
" trows,tcols = small_image.shape[:2]\n",
" # Step 3: Draw the rectangle on large_image\n",
" cv2.rectangle(large_image, (MPx,MPy),(MPx+tcols,MPy+trows),(0,0,255),2)\n",
" cv2.imwrite(\"hasil.png\", large_image)\n",
" print(\"Hasil Yang Sesuai:\")\n",
" print([result, file, img])\n",
"\n",
"similarity.sort(key=lambda x: x[0][0])\n",
"\n",
"print(\"\\nTotal Hasil yang sesuai:\")\n",
"print(similarity[0])"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Cfz8_Sc2Z7EU",
"colab_type": "text"
},
"source": [
"Tungu hingga Selesai"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment