Skip to content
Snippets Groups Projects
Commit 8790eb28 authored by Sam Pasquesi's avatar Sam Pasquesi
Browse files

transfer and crop changes

parent b6def9e5
No related branches found
No related tags found
No related merge requests found
File added
......@@ -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
%% Cell type:code id: tags:
``` python
import os, sys
dir2 = os.path.abspath('')
dir1 = os.path.dirname(dir2)
if not dir1 in sys.path: sys.path.append(dir1)
import shutil
from tkinter.filedialog import askdirectory
from tkinter.messagebox import askyesno
from crop.autocrop import crop
import cv2
```
%% Output
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[5], line 11
9 from crop.autocrop import crop
10 import cv2
---> 11 import torch
ModuleNotFoundError: No module named 'torch'
%% Cell type:code id: tags:
``` python
def readQR(image : cv2.Mat):
qrCodeDetector = cv2.QRCodeDetector()
decoded_text, points, _ = qrCodeDetector.detectAndDecode(image)
if points is not None:
return decoded_text
return ''
```
%% Cell type:code id: tags:
``` python
# 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)
```
%% Output
2023-04-07 15:20:45.255 python[85027:7534542] +[CATransaction synchronize] called within transaction
2023-04-07 15:20:45.259 python[85027:7534542] +[CATransaction synchronize] called within transaction
2023-04-07 15:20:46.328 python[85027:7534542] +[CATransaction synchronize] called within transaction
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
Cell In[16], line 3
1 # find sd path
2 sd_path = askdirectory(message='Select Path To SD')
----> 3 assert sd_path
4 save_dir = askdirectory(message='Select Directory To Save To')
5 assert save_dir
AssertionError:
The 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.
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment