def test_TRAIN_FROM_WEIGHTS_CLI__LOAD_CHECKPOINT_RETURNPREDICT_YOLOv2(): #Test training using pre-generated weights for tiny-yolo-voc #NOTE: This test verifies that the code executes properly, and that the expected checkpoint file (tiny-yolo-voc-20.meta in this case) is generated. # In addition, predictions are generated using the checkpoint file to verify that training completed successfully. testString = "flow --model {0} --load {1} --train --dataset {2} --annotation {3} --epoch 20".format(tiny_yolo_voc_CfgPath, tiny_yolo_voc_WeightPath, os.path.join(buildPath, "test", "training", "images"), os.path.join(buildPath, "test", "training", "annotations")) with pytest.raises(SystemExit): executeCLI(testString) checkpointPath = os.path.join(buildPath, "ckpt", "tiny-yolo-voc-20.meta") assert os.path.exists(checkpointPath), "Expected output checkpoint file: {0} was not found.".format(checkpointPath) #Using trained weights options = {"model": tiny_yolo_voc_CfgPath, "load": 20, "config": generalConfigPath, "threshold": 0.1} tfnet = TFNet(options) #Make sure predictions very roughly match the expected values for image with bike and person imgcv = cv2.imread(trainImgBikePerson["path"]) loadedPredictions = tfnet.return_predict(imgcv) assert compareObjectData(trainImgBikePerson["expected-objects"]["tiny-yolo-voc"], loadedPredictions, trainImgBikePerson["width"], trainImgBikePerson["height"], 0.7, 0.25), "Generated object predictions from training (for image with person on the bike) were not anywhere close to what they are expected to be.\nTraining may not have completed successfully." differentThanExpectedBike = compareObjectData(trainImgBikePerson["expected-objects"]["tiny-yolo-voc"], loadedPredictions, trainImgBikePerson["width"], trainImgBikePerson["height"], 0.01, 0.001) #Make sure predictions very roughly match the expected values for image with horse and person imgcv = cv2.imread(trainImgHorsePerson["path"]) loadedPredictions = tfnet.return_predict(imgcv) assert compareObjectData(trainImgHorsePerson["expected-objects"]["tiny-yolo-voc"], loadedPredictions, trainImgHorsePerson["width"], trainImgHorsePerson["height"], 0.7, 0.25), "Generated object predictions from training (for image with person on the horse) were not anywhere close to what they are expected to be.\nTraining may not have completed successfully." differentThanExpectedHorse = compareObjectData(trainImgHorsePerson["expected-objects"]["tiny-yolo-voc"], loadedPredictions, trainImgHorsePerson["width"], trainImgHorsePerson["height"], 0.01, 0.001) assert not (differentThanExpectedBike and differentThanExpectedHorse), "The generated object predictions for both images appear to be exactly the same as the ones generated with the original weights.\nTraining may not have completed successfully.\n\nNOTE: It is possible this is a fluke error and training did complete properly (try running this build again to confirm) - but most likely something is wrong."
def test_RETURNPREDICT_PBLOAD_YOLOv2(): #Test the .pb and .meta files generated in the previous step #NOTE: This test verifies that the code executes properly, and the .pb and .meta files that were created are able to be loaded and used for inference. # The predictions that are generated will be compared against expected predictions. options = {"pbLoad": pbPath, "metaLoad": metaPath, "threshold": 0.4} tfnet = TFNet(options) imgcv = cv2.imread(testImg["path"]) loadedPredictions = tfnet.return_predict(imgcv) assert compareObjectData(testImg["expected-objects"]["yolo"], loadedPredictions, testImg["width"], testImg["height"], threshCompareThreshold, posCompareThreshold), "Generated object predictions from return_predict() were not within margin of error compared to expected values."
try: vid = YouTube(url).streams.first().download(filepath) except: print "Error" print "Video Downloaded Successfully" option = { 'model': 'cfg/yolo.cfg', 'load': 'bin/yolo.weights', 'threshold': 0.5, #'gpu': 1.0 } #default yolo weights outputfilepath = 'outputs/output.mp4' tfnet = TFNet(option) capture = cv2.VideoCapture(vid) fourcc = cv2.VideoWriter_fourcc(*'XVID') frame_width = int( capture.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height =int( capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) out = cv2.VideoWriter(outputfiepath,fourcc,2.0, (frame_width,frame_height)) # 5.0 is use to give speed by which video will save colors = [tuple(255 * np.random.rand(3)) for i in range(10)] outputtextfile = "lcfile.txt" f = open(outputtextfile,"w") def getFrame(sec): capture.set(cv2.CAP_PROP_POS_MSEC,sec*1000) hasFrames,image = capture.read() return hasFrames #to retrieve frame after specific interval from video
def __init__(self): options = {"model": "/root/object_detection/yolo2/cfg/yolov2-tiny.cfg", "load": "/root/object_detection/yolo2/weights/yolov2-tiny.weights", "threshold": float(os.environ['YOLO_THRESHOLD']), "gpu" : float(os.environ['GPU_MEMORY_FRACTION'])} self.tfnet = TFNet(options)
from darkflow.net.build import TFNet import tensorflow as tf from tensorflow.keras import layers, models import numpy as np import cv2 import imutils from django.conf import settings import os opt = {"pbLoad": os.path.join(settings.MODELS,"yp.pb"), "metaLoad": os.path.join(settings.MODELS,"yp.meta"), "gpu": 0.9} yoloPlate = TFNet(opt) opt = {"pbLoad": os.path.join(settings.MODELS,'yc.pb'), "metaLoad": os.path.join(settings.MODELS,'yc.meta'), "gpu":0.9} yoloCharacter = TFNet(opt) characterRecognition = tf.keras.models.load_model(os.path.join(settings.MODELS,'character_recognition.h5')) def firstCrop(img, pred): pred.sort(key=lambda x: x.get('confidence')) #xtop,ytop,xbottom,ybottom xt = pred[-1].get('topleft').get('x') yt = pred[-1].get('topleft').get('y') xb = pred[-1].get('bottomright').get('x') yb = pred[-1].get('bottomright').get('y') fc = img[yt:yb, xt:xb] cv2.rectangle(img,(xt,yt),(xb,yb),(0,255,0),3) return fc def secondCrop(img): gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(gray,127,255,0) contours,_ = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) areas = [cv2.contourArea(c) for c in contours]
sl_session = tf.Session() with sl_session.as_default(): sl_model = Model(input_shape) prior_util = PriorUtil(sl_model) sl_model.load_weights(weights_path, by_name=True) input_width = 256 input_height = 32 weights_path = 'weights.022.h5' input_size = input_shape[:2] from darkflow.net.build import TFNet import numpy as np yolo9000 = {"model" : "cfg/yolo9000.cfg", "load" : "yolo9000.weights", "threshold": 0.01} tfnet = TFNet(yolo9000) # initialize the output frame and a lock used to ensure thread-safe # exchanges of the output frames (useful for multiple browsers/tabs # are viewing tthe stream) outputFrame = None lock = threading.Lock() # initialize a flask object app = Flask(__name__) # initialize the video stream and allow the camera sensor to # warmup vs = cap time.sleep(2.0)
from darkflow.net.build import TFNet import random import time from flask import Flask, render_template, request, flash, request, redirect, url_for, jsonify import json import requests from statistics import mode import glob # used to detect plate using yolo model options = { "pbLoad": "Plate_recognition_weights/yolo-plate.pb", "metaLoad": "Plate_recognition_weights/yolo-plate.meta", "gpu": 0.9 } yoloPlate = TFNet(options) # used to detect characters on number plate options = { "pbLoad": "Character_recognition_weights/yolo-character.pb", "metaLoad": "Character_recognition_weights/yolo-character.meta", "gpu": 0.9 } yoloCharacter = TFNet(options) characterRecognition = tf.keras.models.load_model('character_recognition.h5') # function that returns the cropped detection with the highest confidence (last in confidence sorted list) # draws rectangle around the highest confidence of license plate detecttions given to it def firstCrop(img, predictions):
import numpy as np import sys from darkflow.net.build import TFNet import cv2 options = { "model": "/home/eric/vehicle-detection/network/cfg/kitti.cfg", "load": -1, #"/home/eric/vehicle-detection/network/weights/yolo.weights", "batch": 8, "epoch": 30, "gpu": 0.9, "train": True, "lr": 1e-6, "annotation": "/home/eric/vehicle-detection/data/KITTI-detection/Annotations/", "dataset": "/home/eric/vehicle-detection/data/KITTI-detection/JPEGImages/" } #os.chdir("../darkflow") tfnet = TFNet(options) tfnet.train()
FLAGS.parseArgs(sys.argv) def _get_dir(dirs): for d in dirs: this = os.path.abspath(os.path.join(os.path.curdir, d)) if not os.path.exists(this): os.makedirs(this) requiredDirectories = [ FLAGS.imgdir, FLAGS.binary, FLAGS.backup, os.path.join(FLAGS.imgdir, 'out') ] if FLAGS.summary: requiredDirectories.append(FLAGS.summary) _get_dir(requiredDirectories) tfnet = TFNet(FLAGS) model = masknet.create_model() model.summary() model.load_weights("weights.hdf5") file = FLAGS.demo SaveVideo = FLAGS.saveVideo if file == 'camera': file = 0 else: pass #assert os.path.isfile(file), \ #'file {} does not exist'.format(file)
import cv2 threshold = 0.03 cfg_path = "cfg/darkflow-ogar-loadweights.cfg" test_img_path = "data/ogars_in_flight/images/Ogar00131.png" darkflow_flags = { "model": cfg_path, "threshold": threshold, "gpu": 0.9, "pbLoad": "built_graph/darkflow-ogar-loadweights.pb", "metaLoad": "built_graph/darkflow-ogar-loadweights.meta", "load": "bin/tiny-yolo-voc.weights", } tfnet = TFNet(darkflow_flags) imgcv = cv2.imread(test_img_path) result = tfnet.return_predict(imgcv) #print("result:", result) if len(result) == 0: print("no objects found") else: best_box = result[0] max_conf = result[0]["confidence"] for box in result: if box["confidence"] > max_conf: best_box = box max_conf = box["confidence"]
label = result['label'] + " " + str(round(confidence, 3)) newImage = cv2.rectangle(newImage, (top_x, top_y), (btm_x, btm_y), (255, 0, 0), 3) newImage = cv2.putText(newImage, label, (top_x, top_y + 100), cv2.FONT_HERSHEY_COMPLEX_SMALL, 5, (0, 0, 0), 3, cv2.LINE_AA) return newImage options = { "model": "cfg/tiny-yolo-voc-3c.cfg", "load": 6000, "gpu": 1.0, "threshold": 0.5 } tfnet2 = TFNet(options) original_img = cv2.imread("dataset/images/IMG_20200119_191711.jpg") original_img = cv2.cvtColor(original_img, cv2.COLOR_BGR2RGB) results = tfnet2.return_predict(original_img) print(results) #print(len(results)) fig, ax = plt.subplots(figsize=(15, 15)) ax.imshow(original_img) fig, ax = plt.subplots(figsize=(15, 15)) ax.imshow(boxing(original_img, results))
import numpy as np import time import tensorflow as tf config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) options = { 'model': 'cfg/yolov2-voc.cfg', 'load': 'bin/yolov2-voc.weights', 'threshold': 0.3, 'gpu': 1.0 } tfnet = TFNet(options) #print modlw arch capture = cv2.VideoCapture(0) colors = [tuple(255 * np.random.rand(3)) for i in range(5)] fourcc = cv2.VideoWriter_fourcc(*'XVID') out11 = cv2.VideoWriter('output.avi', fourcc, 20.0, (1280, 720)) while (capture.isOpened()): stime = time.time() ret, frame = capture.read() if ret: results = tfnet.return_predict(frame) for color, result in zip(colors, results): tl = (result['topleft']['x'], result['topleft']['y']) br = (result['bottomright']['x'], result['bottomright']['y']) label = result['label']
# importing the dependencies import cv2 import matplotlib.pyplot as plt from darkflow.net.build import TFNet import argparse # the cfg file and weights location change this thing according to your model location model = {"model": "cfg/yolov2.cfg", "load": "yolov2.weights", "threshold": 0.4} # creating the object tfnet = TFNet(model) # to get the image path parser = argparse.ArgumentParser() parser.add_argument('--img', type=str, help='path of the image') arg = parser.parse_args() imgcv = cv2.imread(arg.img) # read the image result = tfnet.return_predict( imgcv) # predict the classes and cordinates of the oject # This is for draw the bounding box around the predicted classes tl = [] br = [] labels = [] for i in range(len(result)): topleft = ( result[i]['topleft']['x'], result[i]['topleft']['y'] ) # to get the labels from the predicted class ,it's in the form of dictionary bottomright = (result[i]['bottomright']['x'], result[i]['bottomright']['y'])
import cv2 from darkflow.net.build import TFNet # darkflow 옵션 options = {"model": "cfg/my-tiny-yolo.cfg", "load": -1, "threshold": 0.5} tfnet = TFNet(options) curr_frame = cv2.imread("./test_pic1.jpg") results = tfnet.return_predict(curr_frame) for result in results: cv2.rectangle(curr_frame, (result["topleft"]["x"], result["topleft"]["y"]), (result["bottomright"]["x"], result["bottomright"]["y"]), (255, 0, 0), 4) text_x, text_y = result["topleft"]["x"] - 10, result["topleft"]["y"] - 10 cv2.putText(curr_frame, result["label"], (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2, cv2.LINE_AA) cv2.imwrite("video_capture/test.jpg", curr_frame)
import cv2 from darkflow.net.build import TFNet import matplotlib.pyplot as plt print('Started script.') # define the model options and run options = { 'model': 'bin/prjeddie/yolov2-tiny.cfg', 'load': 'bin/prjeddie/yolov2-tiny.weights', 'threshold': 0.4, 'gpu': 0 } print('Loading darkflow.') tfnet = TFNet(options) print('Read image') # read the color image and covert to RGB img = cv2.imread('media/input/1395447642477-linzerstr.jpg', cv2.IMREAD_COLOR) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) print('Predict the image') # use YOLO to predict the image predictions = tfnet.return_predict(img) # pull out some info from the results print('Render result') for prediction in predictions:
from darkflow.net.build import TFNet import cv2 from io import BytesIO import time import requests from PIL import Image, ImageDraw import numpy as np import glob options = {"model": "cfg/tiny-yolo-voc.cfg", "load": "bin/tiny-yolo-voc.weights", "threshold": 0.15} tfnet = TFNet(options) counter = 0 for filename in glob.glob('birds/*.jpg'): curr_img = Image.open(filename).convert('RGB') curr_imgcv2 = cv2.cvtColor(np.array(curr_img), cv2.COLOR_RGB2BGR) result = tfnet.return_predict(curr_imgcv2) print(result) draw = ImageDraw.Draw(curr_img) for det in result: draw.rectangle([det['topleft']['x'], det['topleft']['y'], det['bottomright']['x'], det['bottomright']['y']], outline=(255, 0, 0)) draw.text([det['topleft']['x'], det['topleft']['y'] - 13], det['label'], fill=(255, 0, 0)) curr_img.save('birds_labeled/%i.jpg' % counter)
dep.left_fit = None dep.right_fit = None dep.right_curverad = 0 dep.left_curverad = 0 dep.first_time = 1 tls = [] brs = [] option = { 'model': 'cfg/tiny-yolo-voc.cfg', 'load': 'bin/tiny-yolo-voc.weights', 'threshold': 0.5, 'gpu': 0 } tfnet = TFNet(option) cap = cv2.VideoCapture("vid.mp4") colors = [tuple(255 * np.random.rand(3)) for i in range(5)] frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) inlane = "no" # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file. out = cv2.VideoWriter('eman.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 30, (frame_width, frame_height)) while (cap.isOpened()): stime = time.time() ret, frame = cap.read() newframe = frame.copy() if ret == False:
class ObjectCountingAPI: def __init__(self, options): self.options = options self.tfnet = TFNet(options) def _write_quantities(self, frame, labels_quantities_dic): for i, (label, quantity) in enumerate(labels_quantities_dic.items()): class_id = [ i for i, x in enumerate(labels_quantities_dic.keys()) if x == label ][0] color = [int(c) for c in COLORS[class_id % len(COLORS)]] cv2.putText( frame, f"{label}: {quantity}", (10, (i + 1) * 35), OBJECTS_ON_FRAME_COUNTER_FONT, OBJECTS_ON_FRAME_COUNTER_FONT_SIZE, color, 2, cv2.FONT_HERSHEY_SIMPLEX, ) def _draw_detection_results(self, frame, results, labels_quantities_dic): for start_point, end_point, label, confidence in results: x1, y1 = start_point class_id = [ i for i, x in enumerate(labels_quantities_dic.keys()) if x == label ][0] color = [int(c) for c in COLORS[class_id % len(COLORS)]] cv2.rectangle(frame, start_point, end_point, color, DETECTION_FRAME_THICKNESS) cv2.putText(frame, label, (x1, y1 - 5), OBJECTS_ON_FRAME_COUNTER_FONT, OBJECTS_ON_FRAME_COUNTER_FONT_SIZE, color, 2) def _convert_detections_into_list_of_tuples_and_count_quantity_of_each_label( self, objects): labels_quantities_dic = {} results = [] for object in objects: x1, y1 = object["topleft"]["x"], object["topleft"]["y"] x2, y2 = object["bottomright"]["x"], object["bottomright"]["y"] confidence = object["confidence"] label = object["label"] try: labels_quantities_dic[label] += 1 except KeyError: labels_quantities_dic[label] = 1 start_point = (x1, y1) end_point = (x2, y2) results.append((start_point, end_point, label, confidence)) return results, labels_quantities_dic def count_objects_on_image(self, frame, targeted_classes=[], output_path="count_people_output.jpg", show=False): objects = self.tfnet.return_predict(frame) if targeted_classes: objects = list( filter(lambda res: res["label"] in targeted_classes, objects)) results, labels_quantities_dic = self._convert_detections_into_list_of_tuples_and_count_quantity_of_each_label( objects) self._draw_detection_results(frame, results, labels_quantities_dic) self._write_quantities(frame, labels_quantities_dic) if show: cv2.imshow("frame", frame) cv2.waitKey() cv2.destroyAllWindows() cv2.imwrite(output_path, frame) # return frame, objects def count_objects_on_video(self, cap, targeted_classes=[], output_path="the_output.avi", show=False): ret, frame = cap.read() fps, height, width = get_output_fps_height_and_width(cap) fourcc = cv2.VideoWriter_fourcc(*'XVID') output_movie = cv2.VideoWriter(output_path, fourcc, fps, (width, height)) while ret: objects = self.tfnet.return_predict(frame) if targeted_classes: objects = list( filter(lambda res: res["label"] in targeted_classes, objects)) results, labels_quantities_dic = self._convert_detections_into_list_of_tuples_and_count_quantity_of_each_label( objects) self._draw_detection_results(frame, results, labels_quantities_dic) self._write_quantities(frame, labels_quantities_dic) output_movie.write(frame) if show: cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break ret, frame = cap.read() cap.release() cv2.destroyAllWindows() def count_objects_crossing_the_virtual_line(self, cap, line_begin, line_end, targeted_classes=[], output_path="the_output.avi", show=False): ret, frame = cap.read() fps, height, width = get_output_fps_height_and_width(cap) fourcc = cv2.VideoWriter_fourcc(*'XVID') output_movie = cv2.VideoWriter(output_path, fourcc, fps, (width, height)) tracker = Sort() memory = {} line = [line_begin, line_end] counter = 0 while ret: objects = self.tfnet.return_predict(frame) if targeted_classes: objects = list( filter(lambda res: res["label"] in targeted_classes, objects)) results, _ = self._convert_detections_into_list_of_tuples_and_count_quantity_of_each_label( objects) # convert to format required for dets [x1, y1, x2, y2, confidence] dets = [[*start_point, *end_point] for (start_point, end_point, label, confidence) in results] np.set_printoptions( formatter={'float': lambda x: "{0:0.3f}".format(100)}) dets = np.asarray(dets) tracks = tracker.update(dets) boxes = [] indexIDs = [] previous = memory.copy() memory = {} for track in tracks: boxes.append([track[0], track[1], track[2], track[3]]) indexIDs.append(int(track[4])) memory[indexIDs[-1]] = boxes[-1] if len(boxes) > 0: i = int(0) for box in boxes: (x, y) = (int(box[0]), int(box[1])) (w, h) = (int(box[2]), int(box[3])) color = [int(c) for c in COLORS[indexIDs[i] % len(COLORS)]] cv2.rectangle(frame, (x, y), (w, h), color, DETECTION_FRAME_THICKNESS) if indexIDs[i] in previous: previous_box = previous[indexIDs[i]] (x2, y2) = (int(previous_box[0]), int(previous_box[1])) (w2, h2) = (int(previous_box[2]), int(previous_box[3])) p0 = (int(x + (w - x) / 2), int(y + (h - y) / 2)) p1 = (int(x2 + (w2 - x2) / 2), int(y2 + (h2 - y2) / 2)) cv2.line(frame, p0, p1, color, 3) if intersect(p0, p1, line[0], line[1]): counter += 1 text = "{}".format(indexIDs[i]) cv2.putText(frame, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) i += 1 cv2.line(frame, line[0], line[1], LINE_COLOR, LINE_THICKNESS) cv2.putText(frame, str(counter), LINE_COUNTER_POSITION, LINE_COUNTER_FONT, LINE_COUNTER_FONT_SIZE, LINE_COLOR, 2) output_movie.write(frame) if show: cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break ret, frame = cap.read() cap.release() cv2.destroyAllWindows()
class FoodLabeller(Labeller): def __init__(self, env_spec, policy, options=None, darkflow_path=None, im_is_rgb=False, **kwargs): super(FoodLabeller, self).__init__(env_spec, policy) if options is None: options = {"model": "cfg/yolo.cfg", "load": "bin/yolov2.weights", "threshold": 1.e-2, "gpu": True} if darkflow_path is None: darkflow_path = os.path.join(str(Path.home()), 'darkflow') old_cwd = os.getcwd() os.chdir(darkflow_path) self._tfnet = TFNet(options) os.chdir(old_cwd) self._labels = [] for key in self._env_spec.goal_spec: if key[-5:] == '_diff': self._labels.append(key[:-5]) self._im_is_rgb = im_is_rgb def label(self, observations, curr_goals): obs_ims = observations[0] goals = [] for obs_im, curr_goal in zip(obs_ims, curr_goals): if self._im_is_rgb: obs_im = obs_im[..., ::-1] # make bgr im_height, im_width, _ = obs_im.shape results = self._tfnet.return_predict(obs_im) boxes = self._get_boxes(results) goal = self._get_goal(boxes, curr_goal, im_height, im_width) goals.append(goal) return goals def _get_boxes(self, results): boxes = [] for result in results: if result['label'] in self._labels: boxes.append(result) return boxes def _get_goal(self, boxes, curr_goal, im_height, im_width): # goal is weight of each goal and then desired center pixel goals = {} min_areas = {} for label in self._labels: goals[label] = (0., 0.) # (in image, normalized diff to middle) min_areas[label] = np.inf im_mid = 0.5 * (im_width - 1.) for box in boxes: label = box['label'] br_x = box['bottomright']['x'] br_y = box['bottomright']['y'] tl_x = box['topleft']['x'] tl_y = box['topleft']['y'] mid_point = 0.5 * np.array((br_x + tl_x, br_y + tl_y)) assert(mid_point[0] >= 0 and mid_point[0] < im_width) assert(mid_point[1] >= 0 and mid_point[1] < im_height) area = (tl_x - br_x) * (tl_y - br_y) if area < min_areas[label]: min_areas[label] = area norm_diff = (im_mid - mid_point[0]) / im_mid goals[label] = (1., norm_diff) goal_keys = list(self._env_spec.goal_spec.keys()) goal = np.array(curr_goal) for label in self._labels: idx = min([idx for idx, k in enumerate(goal_keys) if label in k]) goal[idx:idx+2] = goals[label] # NOTE: assumes in image, normalized diff to middle return goal
def __init__(self, options): self.options = options self.tfnet = TFNet(options)
btm_y = result['bottomright']['y'] confidence = result['confidence'] label = result['label'] + " " + str(round(confidence, 3)) newImage = cv2.rectangle(newImage, (top_x, top_y), (btm_x, btm_y), (255,0,0), 1) newImage = cv2.putText(newImage, label, (top_x, top_y+20), cv2.FONT_HERSHEY_COMPLEX_SMALL , 1, (0, 0, 0), 1, cv2.LINE_AA) size = newImage.shape return (newImage , predictions) # initialize variables for detection options = {"model": "cfg/tiny-yolo-voc-3c.cfg", "load": 8625, "gpu": 1.0, "threshold":0.5} tfnet2 = TFNet(options) ''' for processing multiple images ''' comp = [] img_dir = "result\\app\\input\\" # Enter Directory of all images data_path = os.path.join(img_dir,'*g') files = glob.glob(data_path) for f1 in files: img = cv2.imread(f1) img = cv2.resize(img, (800, 400), interpolation = cv2.INTER_AREA) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) cv2.imwrite('temp.jpg', img) darr = detection(img)
from darkflow.net.build import TFNet import cv2 """ Skript for train model tiny-yolo-voc-2c.cfg, and weights tiny-yolo-voc.weights. This model was trained on a voc-dataset. Config file was modify to 2 classes. If you want to change number of training classes you can change it in tiny-yolo-voc-2c.cfg file. TODO Modify config file to change number classes and add formuls to change number of parametrs. TODO more installation options. If your have a GPU and installed GPU-tensorflow use a "GPU" : 1, in option config. """ options = {"model": "cfg/tiny-yolo-voc-2c.cfg", "load": "bin/tiny-yolo-voc.weights", "batch": 16, "epoch": 500, "train": True, "annotation": "./train/annotation/", "dataset": "./train/images/"} # Train the model tfnet = TFNet(options) tfnet.train() # Built graph to a protobuf file (.pb) tfnet.savepb()
import time import requests import json #from blog import app options = { 'model': 'cfg/yolo.cfg', 'load': 'bin/yolov2.weights', 'threshold': 0.2, 'gpu': 1.0 } print('abc') try: print('inside try') tfnet = TFNet(options) print('3') colors = [tuple(255 * np.random.rand(3)) for _ in range(10)] postUrl = 'http://127.0.0.1:5000/getIp' res = requests.post(postUrl,data={'hello':'hello'}) print(res.text) temp = str(res.text).split(' ') print(temp) url = 'http://192.168.43.1:8080/shot.jpg' #capture0 = cv2.VideoCapture(0) #capture0.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) #capture0.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) capture = cv2.VideoCapture(0) capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640) capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) drawing = False
if image_input is None: print (" Enter the correct Image Path") ################################################################################################################################## # Since we have already run the python script, train_phone_finder.py, the training process of the model is stored as artifacts. # Now, its just loading the trained model and testing it on top of our image. ################################################################################################################################## options = {"model": "cfg/yolo_custom_phone.cfg", "load": -1, "gpu": 1.0 } tfnet = TFNet(options) tfnet.load_from_ckpt() results = tfnet.return_predict(original_img) #################################################################################################################################### # Now, Converting the predicted results to normalized coordinates of the phone location in the image #################################################################################################################################### predictResult(results,columns,rows)
from darkflow.net.build import TFNet import cv2 options = { "model": "cfg/tiny-yolo-voc-3c.cfg", "pbLoad": "built_graph/tiny-yolo-voc-3c.pb", "metaLoad": "built_graph/tiny-yolo-voc-3c.meta" } tfnet = TFNet(options) imgcv = cv2.imread("./validate/1.jpg") result = tfnet.return_predict(imgcv) print(result)
#!/usr/bin/env python3 import io import subprocess import numpy as np import cv2 import tensorflow as tf from darkflow.net.build import TFNet image = '../sample_img/sample_computer.jpg' tfnet = TFNet({"model": "tiny-yolo-voc.cfg", "load": "tiny-yolo-voc.weights"}) with tfnet.graph.as_default(): layers = tfnet.darknet.layers def _get_var(layer, name): return tf.get_variable( 'layer_%d_%s' % (layer, name), shape=layers[layer].wshape[name], initializer=layers[layer].w[name], ) inpt = tf.placeholder(tf.float32, shape=(1, 416, 416, 3)) conv1 = tf.nn.conv2d( inpt, layers[0].w['kernel'], strides=[1, 1, 1, 1], padding='SAME') mean1 = _get_var(0, 'moving_mean')
FLAGS.parseArgs(sys.argv) def _get_dir(dirs): for d in dirs: this = os.path.abspath(os.path.join(os.path.curdir, d)) if not os.path.exists(this): os.makedirs(this) requiredDirectories = [ FLAGS.imgdir, FLAGS.binary, FLAGS.backup, os.path.join(FLAGS.imgdir, 'out') ] if FLAGS.summary: requiredDirectories.append(FLAGS.summary) _get_dir(requiredDirectories) tfnet = TFNet(FLAGS) profile = [(0.0, 0.0)] args = [0, profile] my_save_ckpt(tfnet, *args) aaaa my_pool = ThreadPool() bdir = '../darknet/scripts/coco' all_inps = process_coco( COCO(bdir + "/annotations/person_keypoints_train2014.json"), bdir + "/images/train2014") all_inps += process_coco( COCO(bdir + "/annotations/person_keypoints_val2014.json"),
from darkflow.net.build import TFNet # Training using custom images options = { 'model': 'cfg/MD_2/yolo_SP1.cfg', 'load': 125000, 'trainer': 'adam', 'batch': 5, 'epoch': 30, 'train': True, 'lr': 1e-5, 'save': 5000, 'keep': 3, 'gpu': 0.7, 'annotation': 'annotation/', 'dataset': 'images/', 'backup': 'ckpt/SP1/' } tfnet = TFNet(options) tfnet.load_from_ckpt() tfnet.train()
import numpy as np import cv2 from skimage.measure import compare_ssim import imutils from darkflow.net.build import TFNet options = { "model": "yolov2-tiny.cfg", "load": "yolov2-tiny.weights", "threshold": 0.1, "labels": "labels.txt" } tfnet = TFNet(options) def get_img_objs(original, frame): res = frame.copy() grayA = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY) grayB = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) (score, diff) = compare_ssim(grayA, grayB, full=True) if score > 0.90: return res diff = (diff * 255).astype("uint8") thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
from darkflow.net.build import TFNet options = { "model": "cfg/yolo-voc-hyperion.cfg", "load": "weights/yolo-voc_50000.weights", "threshold": 0.1, "labels": "image.names", "demo": "camera", "gpu": 0.5, "address": 'localhost', "port": 48051, "UDP": True } tfnet = TFNet(options) #imgcv = cv2.imread("./darkflow/sample_img/sample_dog.jpg") #result = tfnet.return_predict(imgcv) tfnet.camera() exit("Demo Stopped, exiting.")
def yolo_in_R(myDir, imageName, threshold): myDir = myDir imageName = imageName import os os.chdir(myDir) # import libraries from darkflow.net.build import TFNet import cv2 import matplotlib.pyplot as plt import matplotlib.image as mpimg # options for tiny-yolo-voc-1class (trained model) options = { 'model': 'cfg/tiny-yolo-voc-1class_toh.cfg', 'load': 1000, 'threshold': threshold, 'gpu': 1.0 } tfnet = TFNet(options) # create image object # global img # img = None img = cv2.imread("Downloaded_images/" + imageName + ".jpg", cv2.IMREAD_COLOR) # img = cv2.imread("sample_img/sample_toh.jpg", cv2.IMREAD_COLOR) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # plt.imshow(img) # plt.show() img img.shape # predict objects in image # result = None result = tfnet.return_predict(img) result # set up bounding boxes tl = (int(result[0]['topleft']['x']), int(result[0]['topleft']['y'])) br = (int(result[0]['bottomright']['x']), int(result[0]['bottomright']['y'])) label = result[0]['label'] # open CV commands img = cv2.rectangle(img, tl, br, (0, 255, 0), 5) # add label to bounding box img = cv2.putText(img, label, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 253, 0), 2) # display image with bounding box # plt.imshow(img) # plt.show() # save image mpimg.imsave("Labeled_images/" + imageName + "_labeled.jpg", img)
import numpy as np import cv2 import imutils import os import sys import datetime datetime.datetime.now() dirname = sys.argv[1] #Load model data options = { "pbLoad": "yolo-plate.pb", "metaLoad": "yolo-plate.meta", "gpu": 1.0 } yoloPlate = TFNet(options) #Crop the bounding box of plate found def firstCrop(img, predictions): predictions.sort(key=lambda x: x.get('confidence')) xtop = predictions[-1].get('topleft').get('x') ytop = predictions[-1].get('topleft').get('y') xbottom = predictions[-1].get('bottomright').get('x') ybottom = predictions[-1].get('bottomright').get('y') firstCrop = img[ytop:ybottom, xtop:xbottom] cv2.rectangle(img, (xtop, ytop), (xbottom, ybottom), (0, 255, 0), 3) return firstCrop #Crop to remove excess parts of vehicle in frame
if __name__ == '__main__': img_origin = cv2.imread('bin/yrm_origin.jpg') # 无弹幕 img_dm = cv2.imread('bin/yrm_dm.jpg') # 有弹幕 if img_origin is None or img_dm is None: print('img_origin is None or img_dm is None') exit(0) options = { 'model': 'cfg/yolo.cfg', 'load': 'bin/yolo.weights', 'threshold': 0.5, 'gpu': 0.7, # 'imgdir': 'sample_img/wa-yolo/', } tfnet = TFNet(options) boxes = tfnet.return_predict(img_origin) if len(boxes) == 0: print('there is no obj') exit(0) for result in boxes: if result['label'] != 'person': continue # 若此box为人 (tx, ty) = (result['topleft']['x'], result['topleft']['y']) (bx, by) = (result['bottomright']['x'], result['bottomright']['y']) img_dm[ty:by, tx:bx] = img_origin[ty:by, tx:bx] // 4 * 3 + img_dm[ty:by, tx:bx] // 4
def createNet(self): return TFNet(self.ops)