def image_recognition(image_path,save_path): image = utils.read_image(image_path) model = core.Model() labels, boxes, scores = model.predict_top(image) fig, ax = plt.subplots(1) # If the image is already a tensor, convert it back to a PILImage # and reverse normalize it if isinstance(image, torch.Tensor): image = reverse_normalize(image) image = transforms.ToPILImage()(image) ax.imshow(image) # Show a single box or multiple if provided if boxes.ndim == 1: boxes = boxes.view(1, 4) if labels is not None and not _is_iterable(labels): labels = [labels] # Plot each box for i in range(boxes.shape[0]): box = boxes[i] width, height = (box[2] - box[0]).item(), (box[3] - box[1]).item() initial_pos = (box[0].item(), box[1].item()) rect = patches.Rectangle(initial_pos, width, height, linewidth=1, edgecolor='r', facecolor='none') if labels: ax.text(box[0] + 5, box[1] - 5, '{}'.format(labels[i]), color='red') ax.add_patch(rect) plt.savefig(save_path)
def search(): btn['state'] = NORMAL #open image button is returened to enabled fracturedataset = core.Dataset( 'trainingimagesgui/' ) #initializes the dataset the parameter is the folder in which to get the images for the dataset mymodel = core.Model([ 'crack' ]) #calls the neural network and it is looking for boxes labelled crack print("model imported" ) #print function to see if the neural network has been called mymodel.fit(fracturedataset) #train the neuralnetwork on the dataset print("finished training custom dataset" ) #prints to show that the neural network has finished training #testing the neural network testimage = utils.read_image( filename ) #read the varible testimage which has the location of what is stored in variable filename print(filename) #test to see the correct location is being pulled endresult = mymodel.predict( testimage) #run the test image on the now trained neural network print( "prediction complete" ) #test to see that the test image has successfully ran on the neural network labels, boxes, scores = endresult #all the parameters the test image will have print(labels) #test to see if the correct label is printed print(boxes) #this will print the coordiantes of the boxes print(scores) #prints a score of how confident the neural network is visualize.show_labeled_image(testimage, boxes, labels) #display the image on the screen
def __getitem__(self, idx): if torch.is_tensor(idx): idx = idx.tolist() # Read in the image from the file name in the 0th column img_name = os.path.join(self._root_dir, self._csv.iloc[idx, 0]) image = read_image(img_name) # Read in xmin, ymin, xmax, and ymax box = self._csv.iloc[idx, 4:] box = torch.tensor(box).view(1, 4) # Read in the label label = self._csv.iloc[idx, 3] targets = {'boxes': box, 'labels': label} # Perform transformations if self.transform: width = self._csv.loc[idx, 'width'] height = self._csv.loc[idx, 'height'] # Apply the transforms manually to be able to deal with # transforms like Resize or RandomHorizontalFlip updated_transforms = [] scale_factor = 1.0 random_flip = 0.0 for t in self.transform.transforms: # Add each transformation to our list updated_transforms.append(t) # If a resize transformation exists, scale down the coordinates # of the box by the same amount as the resize if isinstance(t, transforms.Resize): original_size = min(height, width) scale_factor = original_size / t.size # If a horizontal flip transformation exists, get its probability # so we can apply it manually to both the image and the boxes. elif isinstance(t, transforms.RandomHorizontalFlip): random_flip = t.p # Apply each transformation manually for t in updated_transforms: # Handle the horizontal flip case, where we need to apply # the transformation to both the image and the box labels if isinstance(t, transforms.RandomHorizontalFlip): if random.random() < random_flip: image = transforms.RandomHorizontalFlip(1)(image) # Flip box's x-coordinates box[0, 0] = width - box[0, 0] box[0, 2] = width - box[0, 2] box[0, 0], box[0, 2] = box[0, (2, 0)] else: image = t(image) # Scale down box if necessary targets['boxes'] = (box / scale_factor).long() return image, targets
def run_img(model, img_path=None, image=None, score_filter=0.4): if img_path is None: print("Call run_img function") img_path = op.join(dataset_path, 'chyf_bag_3cl/frames/frame177.jpg') if not os.path.exists(img_path) and image is None: print("No image and path is not valid {}".format(img_path)) return if image is None: image = utils.read_image(img_path) colors = { 'chyf_bag_1': 'b', 'chyf_bag_2': 'r', 'chyf_bag_3': 'g', 'next_box': 'b', 'main_box': 'r', 'out_box': 'g' } visualize.predict_image(model, image, fontsize=5, colors=colors, score_filter=score_filter)
def start_object_detection_job(filename): job_id = str(uuid4()) jobs[job_id] = {'completed': False} job = Thread(daemon=True, target=detect_objects, args=(job_id, utils.read_image(filename))) job.start() return job_id
def transform(model) -> dict: test_imgs = glob.glob("Detection_Test_Set_Img/*.jpg") if len(test_imgs) == 0: raise Exception("Test Set not downloaded") res = {} for img in test_imgs: image = utils.read_image(img) image_name = img.split("/")[-1] predictions = model.predict(image) labels, boxes, scores = predictions mask = scores >= 0.2 res[image_name] = {} res[image_name]["boxes"] = boxes[mask].numpy().tolist() res[image_name]["scores"] = scores[mask].numpy().tolist() print(image_name + " : done") return res
def classificate(self, image_path): image = detecto_utils.read_image(image_path) labels, boxes, scores = self.detector.predict(image) if len(boxes) == 0: return False if DEBUG: detecto_visualize.show_labeled_image(image, boxes, labels) labels_numpy = boxes.detach().cpu().numpy() for box in boxes: detected_logo = image[int(box[1]):int(box[3]), int(box[0]):int(box[2]), :] if self.classifier.classificate(detected_logo): return True return False
def get_predicted_bbs(img_path, model, eps_proba=0.20): """ For a given image path, this function returns all the detected workers' bounding boxes. Function that works only with detecto model types params: - model : detecto model object - eps (float): minimum level of confidence for an object to be detected """ image = utils.read_image(img_path) labels, bbs, scores = model.predict(image) # Get only the boxes with scores higher than eps mask = scores >= eps_proba boxes = {} for i, tensor in enumerate(bbs[mask]): xmin, ymin, xmax, ymax = tensor.numpy() boxes[i] = (xmin, ymin, xmax, ymax) return boxes
def predict_one(self): # Specify the path to your image print(self.imagePath) image = utils.read_image(self.imagePath) predictions = self.model.predict(image) # predictions format: (labels, boxes, scores) labels, boxes, scores = predictions # ['alien', 'bat', 'bat'] print(labels) # xmin ymin xmax ymax # tensor([[ 569.2125, 203.6702, 1003.4383, 658.1044], # [ 276.2478, 144.0074, 579.6044, 508.7444], # [ 277.2929, 162.6719, 627.9399, 511.9841]]) print(boxes) # tensor([0.9952, 0.9837, 0.5153]) print(scores) visualize.show_labeled_image(image, boxes, labels)
def grade_components(basedir): filename = os.path.join(basedir, "top.png") image = utils.read_image(filename) labels, boxes, scores = model.predict(image) labels, boxes, scores = get_best_bounding_boxes(labels, boxes, scores) boxes = boxes.data.detach().numpy() explanation = "" score_dict = dict(regulation=0,capacitance=0,resistance=0,LED=0,power_supply=0) for idx in range(len(labels)): score_dict[labels[idx]] = 1 explanation += "{} detected at {} with confidence {}\n".format(labels[idx], boxes[idx], scores[idx]) score = sum(score_dict.values()) for label in score_dict: if score_dict[label] == 0: explanation += "{} not detected\n".format(label) return score, explanation, labels, boxes, scores
def process_image(dirname, image_path, label): image_name = basename(image_path) image = utils.read_image(image_path) cropped = get_cropped_box(image, label) if cropped is None: return cv2.imwrite(join('.', '_cropped.png'), cv2.cvtColor(cropped, cv2.COLOR_RGB2BGR)) dil = 10 ero = 5 conf_thred = 0.9 print(f'Processing image {image_path}') trimap = generate_trimaps.get_trimap(cropped, label, dil, ero, conf_thred) cv2.imwrite(join(dirname, f'{image_name}_trimap.png'), cv2.cvtColor(trimap, cv2.COLOR_RGB2BGR)) img = read_image('_cropped.png') trimap = read_trimap(join(dirname, f'{image_name}_trimap.png')) fg, bg, alpha = matting.perform_matting(img, trimap) cv2.imwrite( join(dirname, f'{image_name}_fg.png'), fg[:, :, ::-1] * 255, ) cv2.imwrite( join(dirname, f'{image_name}_bg.png'), bg[:, :, ::-1] * 255, ) cv2.imwrite( join(dirname, f'{image_name}_alpha.png'), alpha * 255, ) example_swap_bg = swap_bg(fg[:, :, ::-1] * 255, alpha) cv2.imwrite( join(dirname, f'{image_name}_swapped_bg.png'), example_swap_bg, )
height = 501 # 600 # baseurl = '/bala/projects/inoru/WheelVisualizer/' storageurl = '/storage/custom-detection-dataset/' dummyPath = baseurl + storageurl + "/resized/" img = cv2.imread(image, cv2.IMREAD_COLOR) # img = cv2.resize(img, (0, 0), fx = 0.1, fy = 0.1) img = cv2.resize(img, (width, height)) cv2.imwrite(dummyPath + carid + '_car.png', img) # Specify the path to your image image = utils.read_image(dummyPath + carid + '_car.png') # Specify the path to your image model = core.Model.load(baseurl + storageurl + 'model_weights.pth', ['wheel']) #Getting Prediction Values predictions = model.predict(image) # # predictions format: (labels, boxes, scores) labels, boxes, scores = predictions # visualize.show_labeled_image(image, boxes, labels) # # Blue color in BGR color = (255, 0, 0) # # Line thickness of 2 px thickness = 2
import cv2 from detecto import core, utils, visualize import torch import numpy as np from scipy.spatial.distance import euclidean model = torch.load('cropv2') fname = 'Test.jpg' image = utils.read_image(fname) labels, boxes, scores = model.predict(image) # print(labels) print(boxes) # for i, bbox in enumerate(boxes): # bbox = list(map(int, bbox)) # x_min, y_min, x_max, y_max = bbox # cv2.rectangle(image,(x_min,y_min),(x_max,y_max),(0,255,0),2) # cv2.putText(image, labels[i], (x_min, y_min), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0)) # cv2.imshow('img',image) # cv2.waitKey(0) # cv2.destroyAllWindows() # merge overlap boxes def non_max_suppression_fast(boxes, labels, overlapThresh): # if there are no boxes, return an empty list if len(boxes) == 0: return [] # if the bounding boxes integers, convert them to floats -- # this is important since we'll be doing a bunch of divisions if boxes.dtype.kind == "i": boxes = boxes.astype("float") #
'can_fanta', 'can_sprite', 'chips_can', 'coffee_box', 'cracker', 'cup', 'donut', 'fork', 'gelatin', 'meat', 'mustard', 'newspaper', 'orange', 'pear', 'plate', 'soccer_ball', 'soup', 'sponge', 'sugar', 'toy']) image = utils.read_image('labels/1-7.png') labels, boxes, scores = model.predict(image) predictions = model.predict_top(image) print(labels) print(scores) >>>>>>> 7a083f14c92c0a070ae27a81c752239e2791490c
width, height, linewidth=1, edgecolor='r', facecolor='none') if labels: ax.text(box[0] + 5, box[1] - 5, '{}'.format(labels[i]), color='red') ax.add_patch(rect) plt.show() # detecting 결과 이미지를 현재 작업 디렉토리에 저장 fig.savefig('result.png', dpi=100) plt.close(fig) labels = ['chilli', 'egg', 'pork meat', 'potato', 'pa', 'onion'] model = core.Model.load('ingredients_weights_ver01_0326.pth', labels) image = utils.read_image('test/test_image_02.jpg') # prediction할 이미지의 경로 입력해야함 predictions = model.predict(image) labels, boxes, scores = predictions ## 'result.png'로 저장됨 show_labeled_image(image, boxes, labels) # 이미지에서 검출된 label 정보(list type) detection_class = list(set(labels))
def __getitem__(self, idx): if torch.is_tensor(idx): idx = idx.tolist() # Read in the image from the file name in the 0th column object_entries = self._csv.loc[self._csv['image_id'] == idx] img_name = os.path.join(self._root_dir, object_entries.iloc[0, 0]) image = read_image(img_name) boxes = [] labels = [] for object_idx, row in object_entries.iterrows(): # Read in xmin, ymin, xmax, and ymax box = self._csv.iloc[object_idx, 4:8] boxes.append(box) # Read in the labe label = self._csv.iloc[object_idx, 3] labels.append(label) boxes = torch.tensor(boxes).view(-1, 4) targets = {'boxes': boxes, 'labels': labels} # Perform transformations if self.transform: width = object_entries.iloc[0, 1] height = object_entries.iloc[0, 2] # Apply the transforms manually to be able to deal with # transforms like Resize or RandomHorizontalFlip updated_transforms = [] scale_factor = 1.0 random_flip = 0.0 for t in self.transform.transforms: # Add each transformation to our list updated_transforms.append(t) # If a resize transformation exists, scale down the coordinates # of the box by the same amount as the resize if isinstance(t, transforms.Resize): original_size = min(height, width) scale_factor = original_size / t.size # If a horizontal flip transformation exists, get its probability # so we can apply it manually to both the image and the boxes. elif isinstance(t, transforms.RandomHorizontalFlip): random_flip = t.p # Apply each transformation manually for t in updated_transforms: # Handle the horizontal flip case, where we need to apply # the transformation to both the image and the box labels if isinstance(t, transforms.RandomHorizontalFlip): if random.random() < random_flip: image = transforms.RandomHorizontalFlip(1)(image) for idx, box in enumerate(targets['boxes']): # Flip box's x-coordinates box[0] = width - box[0] box[2] = width - box[2] box[[0, 2]] = box[[2, 0]] targets['boxes'][idx] = box else: image = t(image) # Scale down box if necessary if scale_factor != 1.0: for idx, box in enumerate(targets['boxes']): box = (box / scale_factor).long() targets['boxes'][idx] = box return image, targets
from detecto import core, utils, visualize image = utils.read_image('tests/image1.jpg') model = core.Model.load('un_logo_model.pth', ['un_logo']) #model = core.Model() labels, boxes, scores = model.predict(image) print(scores) visualize.show_labeled_image(image, boxes, labels)
from detecto import core, utils, visualize dataset = core.Dataset('images/') model = core.Model(['fit_avocado', 'unfit_avocado']) model.fit(dataset) image = utils.read_image('images/10.jpg') predictions = model.predict(image) # predictions format: (labels, boxes, scores) labels, boxes, scores = predictions model.save('avocado_weights.pth')
from detecto import core, utils, visualize # Specify the path to your image image = utils.read_image('10827_cc1280_032_BN.jpg')# Specify the path to your image model = core.Model.load('model_weights.pth', ['wheel']) # image = utils.read_image('lorry-transport-services-500x500.jpg') predictions = model.predict(image) # predictions format: (labels, boxes, scores) labels, boxes, scores = predictions visualize.show_labeled_image(image, boxes, labels) # ['alien', 'bat', 'bat'] print(labels) # xmin ymin xmax ymax # tensor([[ 569.2125, 203.6702, 1003.4383, 658.1044], # [ 276.2478, 144.0074, 579.6044, 508.7444], # [ 277.2929, 162.6719, 627.9399, 511.9841]]) print(boxes) # tensor([0.9952, 0.9837, 0.5153]) print(scores)
from detecto import core, utils, visualize import numpy as np dataset = core.Dataset( 'C:/Users/ABC/Desktop/New folder/TDAI/Data/sample') #load data print(len(dataset)) model = core.Model(['top_left', 'top_right', 'bottom_left', 'bottom_right']) #download model losses = model.fit(dataset, epochs=30, verbose=True, learning_rate=0.001) #set parameter formodel and fit model.save('id_card_4_conner.pth') frame = '' image = utils.read_image(frame) labels, boxes, score = model.predict(image) print(labels) print(boxes) ##draw boxes for i, box in enumerate(boxes): box = list(map(int, box)) x_min, y_min, x_max, y_max = box cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2) cv2.putText(image, labels[i], (x_min, y_min), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 255, 0)) #nonmax suppression def non_max_supperession_fast(boxes, labels, overlapThresh):
output.write("image name,copepod length (px),copepod width (px),scale (px/mm),comments,confidence\n") # Load object detection model model = core.Model.load(model_path,['copepod']) if verbose: print("Successfully loaded model.") print(f"Selected images {img_paths}") # Open the loop for img_path in img_paths: # Load in the image raw_image = utils.read_image(img_path) # Get the image name img_name = img_path[(len(input_dir)):] # Zero out our tracking variables square_found = False scale = "NA" copepod_measured = False copepod_length = "NA" copepod_width = "NA" comments = "" #%% Detect grid # Get the grid image
from detecto import core, utils, visualize model = core.Model.load('model/model_weights.pth', ['rust']) image = utils.read_image('dataset/train_images/typewriter-1248089__340.png') predictions = model.predict(image) # predictions format: (labels, boxes, scores) labels, boxes, scores = predictions print(labels) print(boxes) print(scores) visualize.show_labeled_image(image, boxes, labels)
def run_detect(image_name): image = utils.read_image('static/' + image_name) model = core.Model() labels, boxes, scores = model.predict_top(image) return visualize.show_labeled_image(image, boxes, labels)
import os import ocr from detecto.core import Model from detecto.utils import read_image model = Model.load('localization_model.pth', ['digits']) path = argv[1] image_name = argv[2] filename = path + '\\' + image_name data = {} #cascade = cv2.CascadeClassifier(path + "\\Cascades\\cascade.xml") #img = cv2.imread(filename) img = read_image(filename) top_preds = model.predict_top(img) res = top_preds[1].numpy()[0] #numbers = cascade.detectMultiScale(img, 1.1, 3) x, y = int(res[0]), int(res[1]) w = int(res[2]) h = int(res[3]) w = w - x h = h - y img = img[y:y + h, x:x + w] temp_path = "res.png" cv2.imwrite(temp_path, img) good_list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] image, res = ocr.get_output_image(temp_path)
from detecto import core, utils, visualize image = utils.read_image('./images/fruit.jpeg') model = core.Model() labels, boxes, scores = model.predict_top(image) visualize.show_labeled_image(image, boxes, labels)
from detecto import core, utils, visualize image = utils.read_image('val8.jpg') model = core.Model.load('model_weights.pth', ['crop']) predictions = model.predict(image) # predictions format: (labels, boxes, scores) labels, boxes, scores = predictions # ['alien', 'bat', 'bat'] print(labels) # xmin ymin xmax ymax # tensor([[ 569.2125, 203.6702, 1003.4383, 658.1044], # [ 276.2478, 144.0074, 579.6044, 508.7444], # [ 277.2929, 162.6719, 627.9399, 511.9841]]) print(boxes) # tensor([0.9952, 0.9837, 0.5153]) print(scores) visualize.show_labeled_image(image, boxes, labels)
from detecto import core, utils, visualize image = utils.read_image('fruit.jpg') model = core.Model() labels, boxes, scores = model.predict_top(image) visualize.show_labeled_image(image, boxes, labels)
# Getting all the train images path = 'images/' images = [] for file in os.listdir(path): if file.endswith(".png"): images.append(file) # In[6]: images # In[7]: # Testing out the pre-trained model to see if it detects anything in the screenshot image = utils.read_image(path + images[0]) model = core.Model() labels, boxes, scores = model.predict_top(image) visualize.show_labeled_image(image, boxes, labels) # The pre-trained model failed as expected. Though it did detect one of the tiles as a 'book'. # << Label screenshots with labelImg >> # In[10]: # Train model with custom labels dataset = core.Dataset(path) model = core.Model(['tile']) model.fit(dataset)
from detecto import core, utils, visualize image = utils.read_image('cat.jpg') model = core.Model() labels, boxes, scores = model.predict_top(image) visualize.show_labeled_image(image, boxes, labels)
def detecto_m(pic): image = utils.read_image(pic) model = core.Model() labels, boxes, scores = model.predict_top(image) result = visualize.show_labeled_image(image, boxes, labels) return result