# # model for face detection # # use yolo4 tiny to replace mtcnn #detector = MTCNN() CONFIDENCE_THRESHOLD = 0.5 NMS_THRESHOLD = 0.4 class_names = [] with open("classes.txt", "r") as f: class_names = [cname.strip() for cname in f.readlines()] net = cv2.dnn.readNetFromDarknet("yolov4-person_tiny.cfg","yolov4-person_tiny_last.weights") net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA) net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA_FP16) model = cv2.dnn_DetectionModel(net) model.setInputParams(size=(416, 416), scale=1/256) # # model for age and gender inference # model_folder_path = '' # img_folder_path = 'drive/My Drive/Tibame_AIoT_Project/Datasets/cleandataset' # test_img_path = 'drive/My Drive/Tibame_AIoT_Project/test' IMG_SIZE = 224 #onnx_model = "../vgg16_128.onnx" onnx_model = "23-4-resnet_mlp512-128_+600.onnx" sess = rt.InferenceSession(os.path.join(model_folder_path, onnx_model)) input_name = sess.get_inputs()[0].name print("input name", input_name)
number_of_s[l[0]] += 1 course = max(number_of_s, key=number_of_s.get) return course present_candidates = [] fps_start_time = datetime.datetime.now() classNames = [] with open('coco.names', 'r') as f: classNames = f.read().splitlines() print(classNames) thres = 0.5 # Threshold to detect object nms_threshold = 0.2 #(0.1 to 1) 1 means no suppress , 0.1 means high suppress weightsPath = "frozen_inference_graph.pb" configPath = "ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt" net = cv2.dnn_DetectionModel(weightsPath, configPath) net.setInputSize(320, 320) net.setInputScale(1.0 / 127.5) net.setInputMean((127.5, 127.5, 127.5)) net.setInputSwapRB(True) def program(flag): while True: check, frame = video.read() total_people = 0 t = datetime.datetime.now() #frame=sr.upsample(frame) #total_frames = total_frames + 1 print(t.second)
import cv2 import matplotlib.pyplot as plt config_file = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt' frozen_model = 'frozen_inference_graph.pb' model = cv2.dnn_DetectionModel(frozen_model, config_file) classlabels = [] file_name = 'Labels.txt' with open(file_name, 'rt') as fqt: classLabels = fqt.read().rstrip('\n').split('\n') print(classLabels) print(len(classLabels)) model.setInputSize(320, 320) model.setInputScale(1.0 / 127.5) model.setInputMean((127.5, 127.5, 127.5)) model.setInputSwapRB(True) img = cv2.imread('car.jpg') plt.imshow(img) print(plt.imshow(img)) plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) ClassIndex, confidece, bbox = model.detect(img, confThreshold=0.5) print(ClassIndex) font_scale = 3 font = cv2.FONT_HERSHEY_PLAIN for ClassInd, conf, boxes in zip(ClassIndex.flatten(), confidece.flatten(), bbox): cv2.rectangle(img, boxes, (255, 0, 0), 2) cv2.putText(img, classLabels[ClassInd - 1], (boxes[0] + 10, boxes[1] + 40), font,
import cv2 from random import randint with open('Object Detection/coco.names','rt') as f: class_name = f.read().rstrip('\n').split('\n') class_color = [] for i in range(len(class_name)): class_color.append((randint(0,255),randint(0,255),randint(0,255))) modelPath = 'Object Detection/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt' weightPath = 'Object Detection/frozen_inference_graph.pb' net = cv2.dnn_DetectionModel(weightPath,modelPath) net.setInputSize(320,320) net.setInputScale(1.0/ 127.5) net.setInputMean((127.5, 127.5, 127.5)) net.setInputSwapRB(True) cap = cv2.VideoCapture(0) while True: success,img = cap.read() classIds, confs, bbox = net.detect(img,confThreshold=0.5) if len(classIds)!=0: for classId,confidence,box in zip(classIds.flatten(), confs.flatten(), bbox): cv2.rectangle(img,box,color=class_color[classId-1],thickness=2) cv2.putText(img,class_name[classId-1].upper(),(box[0],box[1]-10),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,class_color[classId-1],2) cv2.imshow("Output",img) if cv2.waitKey(1) & 0xff==ord('q'): break
def detect(image_path, save_image=False, display_image=False, benchmark=False, net_config=vision_models.default_object_detection_config): """Detect objects in a given image. Loads an image into a Neural Network for object detection. Args: image_path (str): The path to the image to perform detection on. save_image (bool, optional): Whether an annotated copy of the image should be saved. Saves to same location as image_path, with an updated filename. Defaults to False. display_image (bool, optional): Whether to display the results in a window. Defaults to False (e.g., for terminal execution). benchmark (bool, optional): Whether to record execution time. Times are output to terminal and embedded onto images (if save_image is enabled). Defaults to False. net_config (NeuralNetworkConfig, optional): Additional network configuration. This specifies which model to load and provides any model/network-specific configuration (such as how to normalise inputs). Defaults to models.default_object_detection_config. """ if benchmark: print(net_config.model_dirname) start = timer() # Loading model model_dir = net_config.model_dirname model = '{}res/models/{}/frozen_inference_graph.pb'.format( proj_root_path, model_dir) config = '{}res/models/{}/config.pbtxt'.format(proj_root_path, model_dir) net = cv2.dnn_DetectionModel(model, config) # Configure network net.setInputSize(net_config.input_size_width, net_config.input_size_height) if net_config.input_scale is not None: net.setInputScale(net_config.input_scale) if net_config.input_mean is not None: net.setInputMean(net_config.input_mean) net.setInputSwapRB(net_config.input_swap_RB) # Input image into network image = cv2.imread(image_path) if benchmark: start_NN = timer() classes, confidences, boxes = net.detect(image, confThreshold=0.5) if benchmark: end_NN = timer() # Loop through each potentially detected object for class_id, confidence, box in zip(classes.flatten(), confidences.flatten(), boxes): class_name = COCO_labels[class_id] print("{0:>9} {1}".format(str(confidence), class_name)) if display_image or save_image: cv2.rectangle(image, box, color=(0, 255, 0)) start_y = box[1] # Determine if text will be too near top of image if start_y > 20: text_y = start_y - 10 else: text_y = start_y + 10 # Draw labels a top of bounding boxes cv2.putText(image, class_name, (int(box[0]), int(text_y)), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) if benchmark: end = timer() time_nn = "{:.2f}s".format(end_NN - start_NN) print("Time: {:.2f}s".format(end - start)) print("Time (NN): %s\n" % time_nn) if display_image or save_image: cv2.putText(image, time_nn, (int(5), int(image.shape[0] - 5)), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 0), 2) # Share outputs if save_image: cv2.imwrite(append_filename(args.image), image) if display_image: cv2.imshow('image', image) cv2.waitKey(0) cv2.destroyAllWindows()
import pickle import numpy as np import time import cv2 import os, csv from gensim.models import KeyedVectors word2vec = KeyedVectors.load_word2vec_format('glove.word2vec.300d.txt') LABELS = open("coco.names").read().strip().split("\n") np.random.seed(666) # COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype="uint8") # 导入 YOLO 配置和权重文件并加载网络: net = cv2.dnn_DetectionModel('yolov4.cfg', 'yolov4.weights') # 获取 YOLO 未连接的输出图层 layer = net.getUnconnectedOutLayersNames() with open('id_objfeature.pkl', 'rb') as f: id_objects = pickle.load(f) root = 'FB_pic' dirlist = os.listdir(root) for i in dirlist: # piclist = os.listdir(os.path.join(root, i)) # for j in piclist: # # print(j) id = "FB"+i.split('.')[0] print(id) path = os.path.join(root, i) # try: