Spaces:
Running
Running
File size: 10,611 Bytes
87c3140 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# Run yolov5 on dir
import os
from os import walk
import pandas as pd
import shutil
import subprocess
from detect import *
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)
from machine.general_utils import make_file_names_valid
# pip install cython matplotlib tqdm scipy ipython ninja yacs opencv-python ffmpeg opencv-contrib-python Pillow scikit-image scikit-learn lmfit imutils pyyaml jupyterlab==3
# pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def validateDir(dir):
if not os.path.exists(dir):
os.makedirs(dir)
def readDetailedSampleJPGs(dirDetailed):
f = []
nameList = []
for (dirpath, dirnames, filenames) in walk(dirDetailed):
f.extend(filenames)
break
# print(f)
type(f)
fileList = pd.DataFrame(f)
fileList.columns = ['fname']
df = fileList['fname'].str.split('_', expand=True)
family = df[2]
genus = df[3]
species = df[4]
species = species.str.split('.', expand=True)[0]
nameList = family + "_" + genus + '_' + species
nameList = pd.DataFrame(nameList)
nameList = pd.DataFrame(nameList[0].unique())
nameList = nameList.dropna()
nameList.columns = ['fullname']
return fileList, nameList
def saveDetailedBySpecies(fileList, nameList):
for species in nameList['fullname']:
dirSpecies = os.path.join(dirDetailedBySpecies,species)
validateDir(dirSpecies)
ind = fileList['fname'].str.contains(species, regex=False)
speciesFiles = fileList['fname'][ind]
if len(speciesFiles) == 50:
print(f"{bcolors.BOLD}Species: {species} >>> Number of Images: {len(speciesFiles)}{bcolors.ENDC}")
else:
print(f"{bcolors.WARNING}Species: {species} >>> Number of Images: {len(speciesFiles)}{bcolors.ENDC}")
for img in speciesFiles:
print(f"{bcolors.BOLD} Copied: {img}{bcolors.ENDC}")
shutil.copy(os.path.join(dirDetailed,img), os.path.join(dirSpecies,img))
def runYOLOforDirOfFolders(dirDetailedBySpecies,dirOutBase,nosave,PROJECT,SET,ANNO,VERSION,INCLUDE_SUBDIRS,ANNO_TYPE):
if INCLUDE_SUBDIRS:
f = []
for (dirpath, dirnames, filenames) in walk(dirDetailedBySpecies):
f.extend(dirnames)
break
for species in f:
print(species)
dirSource = os.path.abspath(os.path.join(dirDetailedBySpecies,species))
# dirYOLO = os.path.abspath(os.path.join('yolov5','detect.py'))
dirWeights = os.path.abspath(os.path.join('YOLOv5','yolov5',ANNO,VERSION,'weights','best.pt'))
dirProject = os.path.abspath(os.path.join(dirOutBase,PROJECT,SET))
run(weights=dirWeights,source=dirSource,project=dirProject,name=species,imgsz=(1280, 1280),nosave=nosave,anno_type=ANNO_TYPE)
else:
# f = []
# for (dirpath, dirnames, filenames) in walk(dirDetailedBySpecies):
# f.extend(filenames)
# break
print(SET)
dirSource = dirDetailedBySpecies
species = os.path.basename(dirSource)
# dirYOLO = os.path.abspath(os.path.join('yolov5','detect.py'))
# dirWeights = os.path.join('yolov5','runs','train',ANNO,VERSION,'weights','best.pt')
dirWeights = os.path.join(dirOutBase,'runs','train','Archival_Detector','FieldPrism_Initial','FieldPrism_Initial7','weights','last.pt')
dirProject = os.path.join(dirOutBase,'runs','detect',PROJECT,SET)
run(weights=dirWeights,source=dirSource,project=dirProject,name=species,imgsz=(1280, 1280),nosave=nosave,anno_type=ANNO_TYPE)
def runYOLOforDirOfFolders_PLANT_Botany(dirDetailedBySpecies,dirOutBase,nosave,PROJECT,SET,ANNO,VERSION,INCLUDE_SUBDIRS):
if INCLUDE_SUBDIRS:
f = []
for (dirpath, dirnames, filenames) in walk(dirDetailedBySpecies):
f.extend(dirnames)
break
for species in f:
print(species)
dirSource = os.path.abspath(os.path.join(dirDetailedBySpecies,species))
# dirYOLO = os.path.abspath(os.path.join('yolov5','detect.py'))
dirWeights = os.path.abspath(os.path.join('yolov5','runs','train',ANNO,VERSION,'weights','best.pt'))
dirProject = os.path.abspath(os.path.join(dirOutBase,PROJECT,SET))
run(weights=dirWeights,source=dirSource,project=dirProject,name=species,imgsz=(1280, 1280),nosave=nosave)
else:
# f = []
# for (dirpath, dirnames, filenames) in walk(dirDetailedBySpecies):
# f.extend(filenames)
# break
print(SET)
dirSource = dirDetailedBySpecies
species = os.path.basename(dirSource)
# dirYOLO = os.path.abspath(os.path.join('yolov5','detect.py'))
# dirWeights = os.path.join('yolov5','runs','train',ANNO,VERSION,'weights','best.pt')
# dirWeights = os.path.abspath(os.path.join('YOLOv5','yolov5',ANNO,VERSION,'weights','best.pt'))
dirWeights = os.path.abspath(os.path.join('YOLOv5','yolov5',ANNO,VERSION,'weights','best.pt'))
dirProject = os.path.abspath(os.path.join(dirOutBase,PROJECT,SET))
run(weights=dirWeights,source=dirSource,project=dirProject,name=species,imgsz=(1280, 1280),nosave=nosave)
# for species in f:
# print(species)
# dirSource = os.path.abspath(os.path.join(dirDetailedBySpecies,species))
# dirYOLO = os.path.abspath(os.path.join('yolov5','detect.py'))
# dirWeights = os.path.join('yolov5','runs','train',ANNO,VERSION,'weights','best.pt')
# dirProject = os.path.join(PROJECT,SET)
# if INCLUDE_SUBDIRS:
# run(weights=dirWeights,source=dirSource,project=dirProject,name=species,imgsz=(1280, 1280))
# else:
# run(weights=dirWeights,source=dirSource,project=dirProject,name=SET,imgsz=(1280, 1280))
### Parse the aggregated DetailedSample folder that has 2,500 images
# fileList, nameList = readDetailedSampleJPGs(dirDetailed)
# saveDetailedBySpecies(fileList, nameList)
### Run detect for every folder in dirDetailedBySpecies
# PROJECT = 'MAL'
# SET = 'Detailed'
# dirDetailedBySpecies = os.path.abspath(os.path.join(os.pardir,'Image_Datasets','GBIF_DetailedSample_50Spp_Ind'))
# runYOLOforDirOfFolders(dirDetailedBySpecies,PROJECT,SET,,1)
# dirDetailed = os.path.abspath(os.path.join(os.pardir,'Image_Datasets','GBIF_DetailedSample_50Spp'))
# dirDetailedBySpecies = os.path.abspath(os.path.join(os.pardir,'Image_Datasets','GBIF_DetailedSample_50Spp_Ind'))
# dirDetailedBySpecies = os.path.abspath(os.path.join(os.pardir,'Image_Datasets','GBIF_TargetedSample_Fraxinus'))
### Run detect for TargetedSample
# PREFIX = 'DT_MAL_'
# INCLUDE_SUBDIRS = 0
# PROJECT = 'Botany'#'Cannon'#'MAL_PLANT'
# SET = 'Demo'#'Test_Sheets_PREP'#'Targeted'
# ANNO = 'PREPfull'#'PLANTfull'
# VERSION = 'baseline_all_hypEvolve'#'baseline'
# dirDetailedBySpecies = os.path.abspath(os.path.join('Image_Datasets','Botany_Test_Images'))
# dirOutBase = os.path.abspath(os.path.join('YOLOv5'))
# runYOLOforDirOfFolders(dirDetailedBySpecies,dirOutBase,False,PROJECT,SET,ANNO,VERSION,INCLUDE_SUBDIRS)
# dirDetailedBySpecies = os.path.abspath(os.path.join('Image_Datasets','Cannon','Test_Sheets'))
# dirOutBase = os.path.abspath(os.path.join('YOLOv5'))
# runYOLOforDirOfFolders(dirDetailedBySpecies,dirOutBase,False,PROJECT,SET,ANNO,VERSION,INCLUDE_SUBDIRS)
# dirDetailedBySpecies = os.path.abspath(os.path.join(os.pardir,'Image_Datasets','GBIF_TargetedSample_Quercus_havardii'))
# runYOLOforDirOfFolders(dirDetailedBySpecies,PROJECT,SET,ANNO,VERSION,INCLUDE_SUBDIRS)
# dirDetailedBySpecies = os.path.abspath(os.path.join(os.pardir,'Image_Datasets','GBIF_TargetedSample_Fraxinus'))
# runYOLOforDirOfFolders(dirDetailedBySpecies,PROJECT,SET,ANNO,VERSION,INCLUDE_SUBDIRS)
# dirDetailedBySpecies = os.path.abspath(os.path.join(os.pardir,'Image_Datasets','GBIF_TargetedSample_Juglandaceae'))
# runYOLOforDirOfFolders(dirDetailedBySpecies,PROJECT,SET,ANNO,VERSION,INCLUDE_SUBDIRS)
# dirDetailedBySpecies = os.path.abspath(os.path.join(os.pardir,'Image_Datasets','GBIF_TargetedSample_Lonicera'))
# runYOLOforDirOfFolders(dirDetailedBySpecies,PROJECT,SET,ANNO,VERSION,INCLUDE_SUBDIRS)
# dirDetailedBySpecies = os.path.abspath(os.path.join(os.pardir,'Image_Datasets','GBIF_TargetedSample_Rhus'))
# runYOLOforDirOfFolders(dirDetailedBySpecies,PROJECT,SET,ANNO,VERSION,INCLUDE_SUBDIRS)
INCLUDE_SUBDIRS = 0
PROJECT = 'MAL_FP' #'MAL_PLANT'#'Botany'#'Cannon'#'MAL_PLANT'
SET = 'FieldPrism_Initial' #'Detailed'#'Demo_Plant'#'Test_Sheets_PREP'#'Targeted'
ANNO = 'PREPfull'#'PLANT_Botany'#'PLANTfull'#'PREPfull
VERSION = 'baseline'#'Small_Adoxaceae'#'baseline'
ANNO_TYPE = 'PREP'
# # dirDetailedBySpecies = os.path.abspath(os.path.join('Image_Datasets','FieldPrism_Training_Images','FieldPrism_Training_FS-Poor'))
# dirDetailedBySpecies = 'D:/Dropbox/LM2_Env/Image_Datasets/FieldPrism_Training_Images/FieldPrism_Training_Sheets'
# # dirOutBase = os.path.abspath(os.path.join('YOLOv5'))
# dirOutBase = os.path.dirname(__file__)
# # dirOutBase > PROJECT > SET
# # ML network: ANNO > VERSION
# make_file_names_valid(dirDetailedBySpecies)
# runYOLOforDirOfFolders(dirDetailedBySpecies,dirOutBase,False,PROJECT,SET,ANNO,VERSION,INCLUDE_SUBDIRS,ANNO_TYPE)
# dirDetailedBySpecies = 'D:/Dropbox/LM2_Env/Image_Datasets/FieldPrism_Training_Images/FieldPrism_Training_Outside'
# dirOutBase = os.path.dirname(__file__)
# make_file_names_valid(dirDetailedBySpecies)
# runYOLOforDirOfFolders(dirDetailedBySpecies,dirOutBase,False,PROJECT,SET,ANNO,VERSION,INCLUDE_SUBDIRS,ANNO_TYPE)
dirDetailedBySpecies = 'D:/Dropbox/LM2_Env/Image_Datasets/FieldPrism_Training_Images/FieldPrism_Training_FS-Poor'
dirOutBase = os.path.dirname(__file__)
make_file_names_valid(dirDetailedBySpecies)
runYOLOforDirOfFolders(dirDetailedBySpecies,dirOutBase,False,PROJECT,SET,ANNO,VERSION,INCLUDE_SUBDIRS,ANNO_TYPE)
# dirDetailedBySpecies = 'D:/Dropbox/LM2_Env/Image_Datasets/FieldPrism_Training_Images/REU_Field_QR-Code-Images'
# dirOutBase = os.path.dirname(__file__)
# make_file_names_valid(dirDetailedBySpecies)
# runYOLOforDirOfFolders(dirDetailedBySpecies,dirOutBase,False,PROJECT,SET,ANNO,VERSION,INCLUDE_SUBDIRS,ANNO_TYPE) |