scales = args.scales images = args.images batch_size = int(args.bs) confidence = float(args.confidence) nms_thesh = float(args.nms_thresh) start = 0 CUDA = torch.cuda.is_available() num_classes = 80 classes = load_classes('data/coco.names') model = net(args.configfile) model.load_weights(args.weightsfile) print("Network loaded") model.DNInfo["height"] = args.resolution in_dim = int(model.DNInfo["height"]) if CUDA: model.cuda() model.eval() read_dir = time.time() try: imlist = [osp.join(osp.realpath('.'), images, img) for img in os.listdir(images) if os.path.splitext(img)[1] == '.png' or os.path.splitext(img)[1] =='.jpeg' or os.path.splitext(img)[1] =='.jpg'] except NotADirectoryError:
# global variables to set up for object detection scales = "1,2,3" batch_size = 1 confidence = 0.7 nms_thesh = 0.5 start = 0 CUDA = torch.cuda.is_available() print(CUDA) cc = 0 num_classes = 80 classes = load_classes model = net('cfg/yolov3.cfg') model.load_weights('cfg/yolov3.weights') print("Networkloaded") model.DNInfo["height"] = '256' in_dim = int(model.DNInfo["height"]) # Bounding boxes stored in arrays x_y_values = [] temp = [] # Time stamp of the previous fraame last_accident = 0 # Object dettection code
def calling(): print("detect.py is called") args = arg_parse() scales = args.scales images = args.images batch_size = int(args.bs) confidence = float(args.confidence) nms_thesh = float(args.nms_thresh) start = 0 CUDA = torch.cuda.is_available() num_classes = 80 classes = load_classes('data/coco.names') model = net(args.configfile) model.load_weights(args.weightsfile) print("Network loaded") model.DNInfo["height"] = args.resolution in_dim = int(model.DNInfo["height"]) if CUDA: model.cuda() model.eval() read_dir = time.time() try: imlist = [ osp.join(osp.realpath('.'), images, img) for img in os.listdir(images) if os.path.splitext(img)[1] == '.png' or os.path.splitext(img)[1] == '.jpeg' or os.path.splitext(img)[1] == '.jpg' ] except NotADirectoryError: imlist = [] imlist.append(osp.join(osp.realpath('.'), images)) except FileNotFoundError: print("No with the name {}".format(images)) exit() if not os.path.exists(args.result): os.makedirs(args.result) batches = list( map(preprocess_img, imlist, [in_dim for x in range(len(imlist))])) im_batches = [x[0] for x in batches] orig_ims = [x[1] for x in batches] im_dim_list = [x[2] for x in batches] #Explain im_dim_list = torch.FloatTensor(im_dim_list).repeat(1, 2) if CUDA: im_dim_list = im_dim_list.cuda() leftover = 0 if (len(im_dim_list) % batch_size): leftover = 1 i = 0 write = False objs = {} for batch in im_batches: if CUDA: batch = batch.cuda() #print('batch size => ', batch.size()) with torch.no_grad(): prediction = model(batch, CUDA) prediction = write_results(prediction, confidence, num_classes, nms=True, nms_conf=nms_thesh) if type(prediction) == int: i += 1 continue #Add the current batch number prediction[:, 0] += i * batch_size if not write: output = prediction write = 1 else: output = torch.cat((output, prediction)) for im_num, image in enumerate( imlist[i * batch_size:min((i + 1) * batch_size, len(imlist))]): im_id = i * batch_size + im_num objs = [classes[int(x[-1])] for x in output if int(x[0]) == im_id] print("{0:20s} {1:s}".format("Objects Detected:", " ".join(objs))) print("----------------------------------------------------------") i += 1 if CUDA: torch.cuda.synchronize() try: output except NameError: print("No detections were made") exit() im_dim_list = torch.index_select(im_dim_list, 0, output[:, 0].long()) scaling_factor = torch.min(in_dim / im_dim_list, 1)[0].view(-1, 1) output[:, [1, 3]] -= (in_dim - scaling_factor * im_dim_list[:, 0].view(-1, 1)) / 2 output[:, [2, 4]] -= (in_dim - scaling_factor * im_dim_list[:, 1].view(-1, 1)) / 2 output[:, 1:5] /= scaling_factor for i in range(output.shape[0]): output[i, [1, 3]] = torch.clamp(output[i, [1, 3]], 0.0, im_dim_list[i, 0]) output[i, [2, 4]] = torch.clamp(output[i, [2, 4]], 0.0, im_dim_list[i, 1]) colors = pkl.load(open("pallete", "rb")) def detect_color(c1, c2, img): x1 = int(c1[0]) y1 = int(c1[1]) x2 = int(c2[0]) y2 = int(c2[1]) a = img[y1:y2, x1:x2] color_mean = np.mean( a) ##can we use other techniques? try your self rushil color_percentage = round((color_mean) * (100 / 256), 2) return color_percentage def write(x, batches, results): c1 = tuple(x[1:3].int()) c2 = tuple(x[3:5].int()) ##print("{0} x[1]".format(x[1].int())) img = results[int(x[0])] b, g, r = cv2.split(img) ## cv2.imshow("blue",b) ## cv2.imshow("green",g) ## cv2.imshow("red",r) bb = detect_color(c1, c2, b) l = list(map(lambda x: detect_color(c1, c2, x), [b, g, r])) cls = int(x[-1]) label = "{0}".format(classes[cls]) print(" Event occured for the object recognized as {0}".format(label)) print(" The {0} is {1}% Red {2}%Green {3}%Blue".format( label, l[2], l[1], l[0])) ##label +="{0}% Red {1}%Green {2}%Blue".format(l[2],l[1],l[0]) color = random.choice(colors) cv2.rectangle(img, c1, c2, [0, 0, 0], 3) #change colur if u want t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 1, 1)[0] c2 = c1[0] + t_size[0] + 3, c1[1] + t_size[1] + 4 cv2.rectangle(img, c1, c2, color, -1) cv2.putText(img, label, (c1[0], c1[1] + t_size[1] + 4), cv2.FONT_HERSHEY_PLAIN, 1, [225, 255, 255], 1) return img list(map(lambda x: write(x, im_batches, orig_ims), output)) det_names = pd.Series(imlist).apply( lambda x: "{}/det_{}".format(args.result, x.split("\\")[-1])) list(map(cv2.imwrite, det_names, orig_ims)) torch.cuda.empty_cache() ##deleting all the files from the source folder i.e images so that old images does not pass to object detection images_source_path = glob.glob( r'C:\Users\Kumarpal\Desktop\kavish code\images\*') for f in images_source_path: os.remove(f)
def main(): args = arg_parser() confidence = float(args.confidence) nms_thresh = float(args.nms_thresh) in_dim = int(args.resolution) num_classes = 80 classes = load_classes('data/coco.names') model = net(args.configfile) model.load_weights(args.weightsfile) print("Network Loaded") cap = cv2.VideoCapture(0) if not cap.isOpened(): print("ERROR opening camera") counter = 0 while cap.isOpened(): ret, frame = cap.read() if ret: processed_img, orig_img, im_dim = preprocess_vid(frame, in_dim) im_dim = tf.tile(tf.cast(im_dim, dtype=tf.float32), [2]) if counter % 5 == 0: prediction = model(processed_img) prediction = write_results(prediction, confidence, num_classes, nms=True, nms_conf=nms_thresh) if prediction.shape[-1] != 8: continue var_prediction = tf.Variable(prediction) im_dim_list = tf.tile(tf.expand_dims(im_dim, 0), [var_prediction.shape[0], 1]) scaling_factor = tf.reshape( tf.math.reduce_min(in_dim / im_dim_list, axis=1), (-1, 1)) numpy_prediction = var_prediction.numpy() numpy_prediction[:, [1, 3]] -= ( in_dim - scaling_factor * im_dim_list[:, 0].numpy().reshape(-1, 1)) / 2 numpy_prediction[:, [2, 4]] -= ( in_dim - scaling_factor * im_dim_list[:, 1].numpy().reshape(-1, 1)) / 2 numpy_prediction[:, 1:5] /= scaling_factor numpy_im_dim_list = im_dim_list for i in range(numpy_prediction.shape[0]): numpy_prediction[i, [1, 3]] = np.clip( numpy_prediction[i, [1, 3]], 0.0, numpy_im_dim_list[i, 0]) numpy_prediction[i, [2, 4]] = np.clip( numpy_prediction[i, [2, 4]], 0.0, numpy_im_dim_list[i, 1]) output = tf.convert_to_tensor(numpy_prediction) colors = pkl.load(open("pallete", "rb")) def write_img(x, results): c1 = tuple(tf.cast(x[1:3], dtype=tf.int32)) c2 = tuple(tf.cast(x[3:5], dtype=tf.int32)) img = results cl = int(x[-1]) label = f"{classes[cl]}" color = colors[0] cv2.rectangle(img, c1, c2, color, 1) font_scale = max(1, min(img.shape[0], img.shape[1]) / (1000)) t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, font_scale, 1)[0] c2 = c1[0] + t_size[0] + 3, c1[1] + t_size[1] + 4 cv2.rectangle(img, c1, c2, color, -1) cv2.putText(img, label, (c1[0], c1[1] + t_size[1] + 4), cv2.FONT_HERSHEY_PLAIN, font_scale, [225, 225, 225], 1) return img counter += 1 list(map(lambda x: write_img(x, orig_img), output)) cv2.imshow("Frame", orig_img) if cv2.waitKey(1) & 0xFF == ord('q'): break else: print("ERROR in reading") break