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

main.py

parent 4db52d7d
No related branches found
No related tags found
No related merge requests found
File added
File added
File added
# https://stackoverflow.com/questions/60941012/how-do-i-find-corners-of-a-paper-when-there-are-printed-corners-lines-on-paper-i
from PIL import Image
from pillow_heif import register_heif_opener
register_heif_opener()
import numpy as np
import cv2
import sys
......
import transfer.transfer
\ No newline at end of file
File added
File added
Image Path, Location, Number of Beetles
test
test
test3
%% Cell type:code id: tags:
``` python
import os, sys
import os, sys, io
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
csv_title = 'Image Path, Location, Number of Beetles\n'
csv = 'test3\n'
csv_path = os.path.join('.', 'output.csv')
with open(csv_path, 'a+') as csvfile:
csvfile.seek(0)
if csvfile.readline().strip() == csv_title.strip():
csvfile.seek(0, io.SEEK_END)
else:
csv = csv_title + csv
csvfile.write(csv)
```
%% Cell type:code id: tags:
``` python
```
......
import os
import os, io
import shutil
from tkinter.filedialog import askdirectory
from tkinter.messagebox import askyesno
from ..crop.autocrop import crop
from crop.autocrop import crop
import cv2
from PIL import Image
import torch
def readQR(image : cv2.Mat):
qrCodeDetector = cv2.QRCodeDetector()
decoded_text, points, _ = qrCodeDetector.detectAndDecode(image)
......@@ -13,7 +15,6 @@ def readQR(image : cv2.Mat):
return ''
return decoded_text
# find sd path
sd_path = askdirectory(message='Select Path To SD')
assert sd_path
......@@ -28,7 +29,7 @@ save_paths = []
dir = os.fsencode(sd_path)
for file in os.listdir(dir):
filename = os.fsdecode(file)
if filename.endswith(".png"):
if filename.endswith(".png") or filename.endswith(".jpg"):
# save original image to computer
src_path = os.path.join(sd_path, filename)
dest_path = os.path.join(save_dir, filename)
......@@ -36,27 +37,36 @@ for file in os.listdir(dir):
save_paths.append(dest_path)
#load model
model_path = '../yolov5_model/runs/train/many_small_bugs_final/weights/best.pt'
model_path = 'yolov5_model/runs/train/20beetle_40-non_20dirt_bkg_overlap/weights/best.pt'
model = torch.hub.load('ultralytics/yolov5', 'custom', model_path)
model.eval()
# create output csv
csv = 'Image Path, Location, Number of Beetles\n'
csv_title = 'Image Path, Location, Number of Beetles, Confidence\n'
csv = ''
for path in save_paths:
image = cv2.imread(path)
# read QR
trap_label = readQR(image)
# crop
# crop + save cropped
warped_rgb = crop(image)
Image.fromarray(warped_rgb).save(path+"cropped.jpg")
# run model
result = model(warped_rgb)
num_beetles = results.pandas().xyxy[0].shape[0]
num_beetles = result.pandas().xyxy[0].shape[0]
confidence = result.pandas().xyxy[0]['confidence'].sum()
# update csv
csv += f'{path},{trap_label},{num_beetles}\n'
csv += f'{path},{trap_label},{num_beetles},{confidence}\n'
# save to csv
csv_path = os.path.join(save_dir, 'output.csv')
with open(csv_path, 'a') as csvfile:
csvfile.write(csv)
with open(csv_path, 'a+') as csvfile:
csvfile.seek(0)
if csvfile.readline().strip() == csv_title.strip():
csvfile.seek(0, io.SEEK_END)
else:
csv = csv_title + csv
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