From 8790eb288e58d525f16b49d09e7990b26831f0c3 Mon Sep 17 00:00:00 2001
From: Sam Pasquesi <spasquesi19@gmail.com>
Date: Fri, 14 Apr 2023 13:17:47 -0500
Subject: [PATCH] transfer and crop changes

---
 __init__.py                               |   0
 crop/__pycache__/autocrop.cpython-310.pyc | Bin 0 -> 2530 bytes
 crop/autocrop.py                          |  33 +++--
 transfer/transfer.ipynb                   | 164 ++++++++++++++++++++++
 transfer/transfer.py                      |  59 ++++++++
 5 files changed, 246 insertions(+), 10 deletions(-)
 create mode 100644 __init__.py
 create mode 100644 crop/__pycache__/autocrop.cpython-310.pyc
 create mode 100644 transfer/transfer.ipynb
 create mode 100644 transfer/transfer.py

diff --git a/__init__.py b/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/crop/__pycache__/autocrop.cpython-310.pyc b/crop/__pycache__/autocrop.cpython-310.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..8a84cf1068f38633b4723e8b55a33931df53a32a
GIT binary patch
literal 2530
zcmZuz&2rnw5yoHu@Smb&+9GAiYkM!@!7E!%B{^(mBg?d;Dw$$Pd26lO6bmMX5F`-5
z4nWCr234tYQn~IS4<M#;$t4exN0?Jmdx>6QFU~h0>6KFfYI?fA>F#NC&kw>%rKsWg
z4}a|ZVnx$_P{PfJLii0@+yQYkhxpo<9225=-PgzTm=cZA_gaQf-#9igLVfGl%KF@K
z&M7*Ullvn%&V$Q41#kt2_O({&8a}lak>>u`b{UvDXRa3~j1NwjHyVTy3mETQ2T*%4
zRG}=MhhXw(J81D1MiT9c2+&h~s;4xeSB69I>Vh;hmrhA)WEf;e&twx8rl7(Q=9H$E
z(5|ehmgXEPa>8=RnepY!XAVAd@R^5xUgRD4g?<52K@`*s7%!rgR<)_&m`yD$Vy3cV
zCG@lcyAm|3LTN0C5-iJ^40>AoC@%{oub|z6MkT3>%6sDE&U7A$ibI-OK+!ATkkYEC
zJlB4=lrF*3ZLx%JtBIPZo*DelqK46>boslth~bXVFb1zzt71hgyD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gK9}hNP#h7-}4a1oE;W^`*7a>23BYVg;yL+$uPk;Gj
z)8*kLdi40oqv+%^2Jk43wjOUi-5l~T+O#K0sF*LXh7HB2Js3M6RZ^!lQl)uX2CdR{
zv>K_A6}nCgk|#wp1F!yvBCV4$T_FwHAPs$uG(O~w26=8ihfa|gG*23&3T~Yil^(6?
zRbuG|5dI1i4GoaRqqeG&v&(k`COd4@6=5VgE>vC6_cX=j8rOuGT0v8o-{Oi*bGSrt
zF^k-k;OfPZhbt3DVTR@Bn0^DZW|-1(G#ps8^DeIn`o(GFzFVYP2`!6P5r(w9F_wUn
zMa<P1B?}`Sj%SB~5!>giRluPzb|V|L&O0nl6vwxq%AYFoL>W=e68>8ysxRa{MN}zg
z<OrNBt1`TR?-!oWR3+jX2vSE3-6BQ3PVnmdTYxQ{t7N@s6(#NNAI#z^MV^m6k^Say
zb<j#Pn2e)Kz6^f$F*Lr6XJ4Yc+z22GC5s~e<v4UEKKr#gXk+}oX=S}kEEHY6MOu7q
GRQ?NYQJdlb

literal 0
HcmV?d00001

diff --git a/crop/autocrop.py b/crop/autocrop.py
index 92f5409..dd1f6ac 100644
--- a/crop/autocrop.py
+++ b/crop/autocrop.py
@@ -8,8 +8,7 @@ import cv2
 import sys
 
 # does not work with heif rn
-def crop(imgpath, destpath, sensitivity=110, debug=False):
-    img = cv2.imread(imgpath)
+def crop(img, sensitivity=110, debug=False):
     hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
 
     # Define lower and uppper limits of what we call "white-ish"
@@ -94,29 +93,43 @@ def crop(imgpath, destpath, sensitivity=110, debug=False):
     warped_rgb = cv2.cvtColor(warped, cv2.COLOR_BGR2RGB)
     polygon_rgb = cv2.cvtColor(polygon, cv2.COLOR_BGR2RGB)
     
-    Image.fromarray(warped_rgb).save(destpath)
-    if debug:
+    if not debug:
+        return warped_rgb
+    return warped_rgb, page, polygon_rgb
+
+def cropFromFile(imgpath, destpath, sensitivity=110, debug=False):
+    img = cv2.imread(imgpath)
+    ret = crop(img, sensitivity, debug)
+
+    if not debug:
+        warped_rgb = ret
+    else:
+        warped_rgb, page, polygon_rgb = ret
         Image.fromarray(page).save(destpath+"page.jpg")
         Image.fromarray(polygon_rgb).save(destpath+"polygon.jpg")
+    
+    Image.fromarray(warped_rgb).save(destpath)
 
+'''
 if len(sys.argv) < 3 or len(sys.argv) > 5 :
     sys.print("Format: python3 autocrop.py imgpath destpath [sensitivity=110] [debug=False]")
     exit(1)
 
 if len(sys.argv) == 3:
-    crop(sys.argv[1], sys.argv[2])
+    cropFromFile(sys.argv[1], sys.argv[2])
 
 elif len(sys.argv) == 5:
     if sys.argv[4] == "True":
-        crop(sys.argv[1], sys.argv[2], int(sys.argv[3]), True)
+        cropFromFile(sys.argv[1], sys.argv[2], int(sys.argv[3]), True)
     else:
-        crop(sys.argv[1], sys.argv[2], int(sys.argv[3]), False)
+        cropFromFile(sys.argv[1], sys.argv[2], int(sys.argv[3]), False)
 
 else:
     try:
-        crop(sys.argv[1], sys.argv[2], sensitivity=int(sys.argv[3]))
+        cropFromFile(sys.argv[1], sys.argv[2], sensitivity=int(sys.argv[3]))
     except ValueError:
         if sys.argv[3] == "True":
-            crop(sys.argv[1], sys.argv[2], debug=True)
+            cropFromFile(sys.argv[1], sys.argv[2], debug=True)
         else:
-            crop(sys.argv[1], sys.argv[2], debug=False)
\ No newline at end of file
+            cropFromFile(sys.argv[1], sys.argv[2], debug=False)
+'''
\ No newline at end of file
diff --git a/transfer/transfer.ipynb b/transfer/transfer.ipynb
new file mode 100644
index 0000000..0039c8b
--- /dev/null
+++ b/transfer/transfer.ipynb
@@ -0,0 +1,164 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "ModuleNotFoundError",
+     "evalue": "No module named 'torch'",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
+      "Cell \u001b[0;32mIn[5], line 11\u001b[0m\n\u001b[1;32m      9\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mcrop\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mautocrop\u001b[39;00m \u001b[39mimport\u001b[39;00m crop\n\u001b[1;32m     10\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mcv2\u001b[39;00m\n\u001b[0;32m---> 11\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mtorch\u001b[39;00m\n",
+      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'torch'"
+     ]
+    }
+   ],
+   "source": [
+    "import os, sys\n",
+    "dir2 = os.path.abspath('')\n",
+    "dir1 = os.path.dirname(dir2)\n",
+    "if not dir1 in sys.path: sys.path.append(dir1)\n",
+    "\n",
+    "import shutil\n",
+    "from tkinter.filedialog import askdirectory\n",
+    "from tkinter.messagebox import askyesno\n",
+    "from crop.autocrop import crop\n",
+    "import cv2"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def readQR(image : cv2.Mat):\n",
+    "    qrCodeDetector = cv2.QRCodeDetector()\n",
+    "    decoded_text, points, _ = qrCodeDetector.detectAndDecode(image)\n",
+    "    if points is not None:\n",
+    "        return decoded_text\n",
+    "    return ''"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 16,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "2023-04-07 15:20:45.255 python[85027:7534542] +[CATransaction synchronize] called within transaction\n",
+      "2023-04-07 15:20:45.259 python[85027:7534542] +[CATransaction synchronize] called within transaction\n",
+      "2023-04-07 15:20:46.328 python[85027:7534542] +[CATransaction synchronize] called within transaction\n"
+     ]
+    },
+    {
+     "ename": "AssertionError",
+     "evalue": "",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[0;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
+      "Cell \u001b[0;32mIn[16], line 3\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[39m# find sd path\u001b[39;00m\n\u001b[1;32m      2\u001b[0m sd_path \u001b[39m=\u001b[39m askdirectory(message\u001b[39m=\u001b[39m\u001b[39m'\u001b[39m\u001b[39mSelect Path To SD\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[0;32m----> 3\u001b[0m \u001b[39massert\u001b[39;00m sd_path\n\u001b[1;32m      4\u001b[0m save_dir \u001b[39m=\u001b[39m askdirectory(message\u001b[39m=\u001b[39m\u001b[39m'\u001b[39m\u001b[39mSelect Directory To Save To\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[1;32m      5\u001b[0m \u001b[39massert\u001b[39;00m save_dir\n",
+      "\u001b[0;31mAssertionError\u001b[0m: "
+     ]
+    },
+    {
+     "ename": "",
+     "evalue": "",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[1;31mThe Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
+     ]
+    }
+   ],
+   "source": [
+    "# find sd path\n",
+    "sd_path = askdirectory(message='Select Path To SD')\n",
+    "assert sd_path\n",
+    "save_dir = askdirectory(message='Select Directory To Save To')\n",
+    "assert save_dir\n",
+    "\n",
+    "# confirmation\n",
+    "assert askyesno(message=f'Confirm Image Transfer:\\n{sd_path}\\n\\u2192\\n{save_dir}')\n",
+    "\n",
+    "# move imgs from sd to pc\n",
+    "save_paths = []\n",
+    "dir = os.fsencode(sd_path)\n",
+    "for file in os.listdir(dir):\n",
+    "    filename = os.fsdecode(file)\n",
+    "    if filename.endswith(\".png\"):\n",
+    "        # save original image to computer\n",
+    "        src_path = os.path.join(sd_path, filename)\n",
+    "        dest_path = os.path.join(save_dir, filename)\n",
+    "        shutil.move(src_path, dest_path)\n",
+    "        save_paths.append(dest_path)\n",
+    "\n",
+    "# create output csv\n",
+    "csv = 'Image Path, Location, Number of Beetles\\n'\n",
+    "for path in save_paths:\n",
+    "    image = cv2.imread(path)\n",
+    "    \n",
+    "    # read QR\n",
+    "    trap_label = readQR(image)\n",
+    "\n",
+    "    # crop\n",
+    "    warped_rgb = crop(image)\n",
+    "\n",
+    "    # run model\n",
+    "    model_path = ''\n",
+    "    model = torch.hub.load(model_path)\n",
+    "    result = model(path)\n",
+    "\n",
+    "    # update csv\n",
+    "    csv += f'{path},{trap_label},{result}\\n'\n",
+    "\n",
+    "csv_path = os.path.join(save_dir, 'output.csv')\n",
+    "with open(csv_path, 'a') as csvfile:\n",
+    "    csvfile.write(csv)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "tf",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.10.9"
+  },
+  "orig_nbformat": 4
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/transfer/transfer.py b/transfer/transfer.py
new file mode 100644
index 0000000..a3f65da
--- /dev/null
+++ b/transfer/transfer.py
@@ -0,0 +1,59 @@
+import os
+import shutil
+from tkinter.filedialog import askdirectory
+from tkinter.messagebox import askyesno
+from ..crop.autocrop import crop
+import cv2
+import torch
+
+def readQR(image : cv2.Mat):
+    qrCodeDetector = cv2.QRCodeDetector()
+    decoded_text, points, _ = qrCodeDetector.detectAndDecode(image)
+    if points is None:
+        return ''
+    return decoded_text
+
+
+# find sd path
+sd_path = askdirectory(message='Select Path To SD')
+assert sd_path
+save_dir = askdirectory(message='Select Directory To Save To')
+assert save_dir
+
+# confirmation
+assert askyesno(message=f'Confirm Image Transfer:\n{sd_path}\n\u2192\n{save_dir}')
+
+# move imgs from sd to pc
+save_paths = []
+dir = os.fsencode(sd_path)
+for file in os.listdir(dir):
+    filename = os.fsdecode(file)
+    if filename.endswith(".png"):
+        # save original image to computer
+        src_path = os.path.join(sd_path, filename)
+        dest_path = os.path.join(save_dir, filename)
+        shutil.move(src_path, dest_path)
+        save_paths.append(dest_path)
+
+# create output csv
+csv = 'Image Path, Location, Number of Beetles\n'
+for path in save_paths:
+    image = cv2.imread(path)
+    
+    # read QR
+    trap_label = readQR(image)
+
+    # crop
+    warped_rgb = crop(image)
+
+    # run model
+    model_path = ''
+    model = torch.hub.load(model_path)
+    result = model(path)
+
+    # update csv
+    csv += f'{path},{trap_label},{result}\n'
+
+csv_path = os.path.join(save_dir, 'output.csv')
+with open(csv_path, 'a') as csvfile:
+    csvfile.write(csv)
\ No newline at end of file
-- 
GitLab