From def22d6cbca575f638933d32670992d83e7d5a73 Mon Sep 17 00:00:00 2001 From: Zhiying Li <zhiyingli@g.ucla.edu> Date: Mon, 1 Nov 2021 20:01:49 -0700 Subject: [PATCH] Update the Ball_detection ModifyCore() Run it only once --> run multiple times --- ...ive_full_detection_detecto_with_ESP_CAM.py | 26 +++++++--------- Code/Control/Laptop_Code/README.md | 4 +++ .../ball_detection/ball_detection.py | 31 ++++++++++++++++++- .../ball_detection/ball_detection_full.py | 8 ++--- Code/Control/Laptop_Code/main_keyboard.py | 3 +- 5 files changed, 52 insertions(+), 20 deletions(-) diff --git a/Code/Ball_Detection/PyTorch_with_ESPCAM/live_full_detection_detecto_with_ESP_CAM.py b/Code/Ball_Detection/PyTorch_with_ESPCAM/live_full_detection_detecto_with_ESP_CAM.py index 45d0f4b..02162c2 100644 --- a/Code/Ball_Detection/PyTorch_with_ESPCAM/live_full_detection_detecto_with_ESP_CAM.py +++ b/Code/Ball_Detection/PyTorch_with_ESPCAM/live_full_detection_detecto_with_ESP_CAM.py @@ -18,7 +18,7 @@ from matplotlib import patches from detecto import core, utils from torchvision import transforms from matplotlib import pyplot as plt -from IPython.display import Image, display, Javascript +# from IPython.display import Image, display, Javascript distanceDetect = __import__('distance-detection-torch.distance-detection-torch', fromlist = ['distanceDetect']).distanceDetect print("torch.cuda.is_available() = ", torch.cuda.is_available()) @@ -26,22 +26,20 @@ print("torch.cuda.is_available() = ", torch.cuda.is_available()) #change the IP address below according to the #IP shown in the Serial monitor of Arduino code # url='http://192.168.1.107/cam-lo.jpg' -url='http://192.168.4.1/cam-hi.jpg' +# url='http://192.168.4.1/cam-hi.jpg' # url='http://192.168.1.107/cam-mid.jpg' - +url='http://192.168.0.230/cam-hi.jpg' #### Modify Detecto Core to include possibility of other base models def modifyCore(): - cModLineNums = [221, 254] + cModLineNums = [221, 253, 254] cAddLineNums = [254, 256, 257, 258, 259] # REPLACABLE LINE FOR DIFFERENT LOCAL COMPUTER DEVICES - coreFile = 'C:\\Users\\uclal\\.conda\\envs\\FORAY\\Lib\\site-packages\\detecto\\core.py' - - - print('debug') + coreFile = 'C:\\Users\\uclal\\.conda\\envs\\foray2\\Lib\\site-packages\\detecto\\core.py' cModLineVals = [" def __init__(self, classes=None, device=None, pretrained=True, modelname=\'fasterrcnn_resnet50_fpn\'):\n", + " # Load a model pre-trained on COCO - User-Modified", " self._model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=pretrained)\n"] cAddLineVals = [" if modelname == \'fasterrcnn_resnet50_fpn\':\n", " elif modelname == \'fasterrcnn_mobilenet_v3_large_fpn\':\n", @@ -146,7 +144,7 @@ def detectLive(model, showSight = True): #print(frame) gbx = -1 gby = -1 - + header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36."} req = Request(url, headers=header) img_resp = urlopen(req, timeout=60) @@ -265,13 +263,13 @@ distWtsFile = '.\distance-detection-torch\distance-detection-weights-3-2.0sd-202 modelAction = 'load' # 'load' to load previous model, 'new' to train new model (only possible on Colab) #FOR THE NEXT LINE : UNCOMMENT the first time you run, COMMENT OUT after the first time -#modifyCore() +# modifyCore() ######### -# model = returnModel(modelAction, device, trainLoc, labelSet, modelLoc, modelFile) -# # # ## Testing (using Live Streaming) -# while True: -# detectLive(model) +model = returnModel(modelAction, device, trainLoc, labelSet, modelLoc, modelFile) +# # ## Testing (using Live Streaming) +while True: + detectLive(model) # ## Testing (using Untrained Images) # for file in sorted(glob.glob(imageLoc + '/test/*.jpg')): diff --git a/Code/Control/Laptop_Code/README.md b/Code/Control/Laptop_Code/README.md index fbdeb70..845e17a 100644 --- a/Code/Control/Laptop_Code/README.md +++ b/Code/Control/Laptop_Code/README.md @@ -70,8 +70,12 @@ pip install opencv-python conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch # install pupil-apriltags pip install pupil-apriltags +# install detecto +pip install detecto==1.2.1 ``` + + ## 3 Main_code There are two files about main code: diff --git a/Code/Control/Laptop_Code/ball_detection/ball_detection.py b/Code/Control/Laptop_Code/ball_detection/ball_detection.py index 7d5728d..ef8c377 100644 --- a/Code/Control/Laptop_Code/ball_detection/ball_detection.py +++ b/Code/Control/Laptop_Code/ball_detection/ball_detection.py @@ -18,7 +18,6 @@ from matplotlib import patches from detecto import core, utils from torchvision import transforms from matplotlib import pyplot as plt -from IPython.display import Image, display, Javascript distanceDetect = __import__('ball_detection.distance-detection-torch.distance-detection-torch', fromlist = ['distanceDetect']).distanceDetect # print("torch.cuda.is_available() = ", torch.cuda.is_available()) @@ -27,6 +26,36 @@ distanceDetect = __import__('ball_detection.distance-detection-torch.distance-de url='http://192.168.4.1/cam-hi.jpg' # url='http://192.168.1.107/cam-mid.jpg' +#### Modify Detecto Core to include possibility of other base models +def modifyCore(): + cModLineNums = [221, 253, 254] + cAddLineNums = [254, 256, 257, 258, 259] + + # REPLACABLE LINE FOR DIFFERENT LOCAL COMPUTER DEVICES + coreFile = 'C:\\Users\\uclal\\.conda\\envs\\foray2\\Lib\\site-packages\\detecto\\core.py' + + cModLineVals = [" def __init__(self, classes=None, device=None, pretrained=True, modelname=\'fasterrcnn_resnet50_fpn\'):\n", + " # Load a model pre-trained on COCO - User-Modified", + " self._model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=pretrained)\n"] + cAddLineVals = [" if modelname == \'fasterrcnn_resnet50_fpn\':\n", + " elif modelname == \'fasterrcnn_mobilenet_v3_large_fpn\':\n", + " self._model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_fpn(pretrained=pretrained)\n", + " elif modelname == \'fasterrcnn_mobilenet_v3_large_320_fpn\':\n", + " self._model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_320_fpn(pretrained=pretrained)\n", + " else:", + " return ValueError('Unknown Pretrained Model')"] + coreRead = open(coreFile, "r") + coreLines = coreRead.readlines() + if coreLines[253][-14:-1] != 'User-Modified': + for count, cModLineNum in enumerate(cModLineNums): + coreLines[cModLineNum] = cModLineVals[count] + for count, cAddLineNum in enumerate(cAddLineNums): + coreLines.insert(cAddLineNum, cAddLineVals[count]) + coreRead.close() + coreWrite = open(coreFile, "w") + coreWrite.writelines(coreLines) + coreWrite.close() + def modelName(type = 1): if type == 1: return 'fasterrcnn_resnet50_fpn' diff --git a/Code/Control/Laptop_Code/ball_detection/ball_detection_full.py b/Code/Control/Laptop_Code/ball_detection/ball_detection_full.py index 1666535..ee9dcea 100644 --- a/Code/Control/Laptop_Code/ball_detection/ball_detection_full.py +++ b/Code/Control/Laptop_Code/ball_detection/ball_detection_full.py @@ -18,7 +18,6 @@ from matplotlib import patches from detecto import core, utils from torchvision import transforms from matplotlib import pyplot as plt -from IPython.display import Image, display, Javascript distanceDetect = __import__('ball_detection.distance-detection-torch.distance-detection-torch', fromlist = ['distanceDetect']).distanceDetect print("torch.cuda.is_available() = ", torch.cuda.is_available()) @@ -31,13 +30,14 @@ url='http://192.168.4.1/cam-hi.jpg' #### Modify Detecto Core to include possibility of other base models def modifyCore(): - cModLineNums = [221, 254] + cModLineNums = [221, 253, 254] cAddLineNums = [254, 256, 257, 258, 259] # REPLACABLE LINE FOR DIFFERENT LOCAL COMPUTER DEVICES - coreFile = 'C:\\Users\\uclal\\.conda\\envs\\FORAY\\Lib\\site-packages\\detecto\\core.py' + coreFile = 'C:\\Users\\uclal\\.conda\\envs\\foray2\\Lib\\site-packages\\detecto\\core.py' cModLineVals = [" def __init__(self, classes=None, device=None, pretrained=True, modelname=\'fasterrcnn_resnet50_fpn\'):\n", + " # Load a model pre-trained on COCO - User-Modified", " self._model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=pretrained)\n"] cAddLineVals = [" if modelname == \'fasterrcnn_resnet50_fpn\':\n", " elif modelname == \'fasterrcnn_mobilenet_v3_large_fpn\':\n", @@ -238,7 +238,7 @@ distWtsFile = './ball_detection/distance-detection-torch/distance-detection-weig modelAction = 'load' # 'load' to load previous model, 'new' to train new model (only possible on Colab) #FOR THE NEXT LINE : UNCOMMENT the first time you run, COMMENT OUT after the first time -#modifyCore() +modifyCore() ######### # model = returnModel(modelAction, device, trainLoc, labelSet, modelLoc, modelFile) diff --git a/Code/Control/Laptop_Code/main_keyboard.py b/Code/Control/Laptop_Code/main_keyboard.py index 553a0b2..a1e41ee 100644 --- a/Code/Control/Laptop_Code/main_keyboard.py +++ b/Code/Control/Laptop_Code/main_keyboard.py @@ -8,7 +8,7 @@ from constants import * from ESP32_AT.imageTread_AT import get_AT_6DOF_info global ml,esp_cam_on,openmv_on -ml = 0 +ml = 1 esp_cam_on = 0 openmv_on = 1 if ml == 1: @@ -629,6 +629,7 @@ if __name__ == '__main__': # Loading the PyTorch ML model for ball detection if ml == 1: + ball_detection.modifyCore() model = ball_detection.returnModel(device, labelSet, modelLoc, modelFile) # =========== DECLARE VARIABLES =========== -- GitLab