def run(self): self.imageCount = 0 self.capture = cv2.VideoCapture(self.video_path) while True: ret, frame = self.capture.read() if not ret: break im = darknet.nparray_to_image(frame) boxes = darknet.detect(self.net, self.meta, im) for i in range(len(boxes)): score = boxes[i][1] label = boxes[i][0] xmin = boxes[i][2][0] - boxes[i][2][2] / 2 ymin = boxes[i][2][1] - boxes[i][2][3] / 2 xmax = boxes[i][2][0] + boxes[i][2][2] / 2 ymax = boxes[i][2][1] + boxes[i][2][3] / 2 cv2.rectangle(frame, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (0, 255, 0), 2) cv2.putText(frame, str(label), (int(xmin), int(ymin)), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.8, color=(0, 255, 255), thickness=2) cv2.imwrite("./temps/temp" + str(self.imageCount) + ".jpg", frame) self.add_image.emit("./temps/temp" + str(self.imageCount) + ".jpg") time.sleep(0.05) self.imageCount += 1
def videoDetection(self): answer = QMessageBox.warning( self, "注意", "如果您上次使用了摄像头检测或视频检测后没有导出,当前操作会导致数据丢失,请确认是否要继续!", QMessageBox.No | QMessageBox.Yes, QMessageBox.Yes) if answer == QMessageBox.No: return if os._exists("./temps"): shutil.rmtree("./temps") os.mkdir("./temps") fname, _ = QFileDialog.getOpenFileName(self, '请选择图片文件', ".", "(*.mp4)") fnameSplit = fname.split('.') index = len(fnameSplit) - 1 typeSuffix = fnameSplit[index] #文件名后缀 if typeSuffix != "mp4": QMessageBox.critical(self, "文件类型错误", "您未指定视频或指定的文件不是mp4文件,请确认!", QMessageBox.Yes, QMessageBox.Yes) else: #正确的图片路径 cap = cv2.VideoCapture(fname) while True: ret, frame = cap.read() if ret: im = darknet.nparray_to_image(frame) r = darknet.detect(self.net, self.meta, im) else: break cap.release() cv2.destroyAllWindows()
def yolov3_detect(): t1 = time.time() temp_jpg_file_path = "/home/li/webapi/tmp/" image_seq = "default" try: image_seq = flask.request.form.get('sequence') image_file = flask.request.files['image'] t2 = time.time() - t1 print(t2) #print(image_seq) #print(image_content) if image_file: temp_jpg_file = temp_jpg_file_path + "temp.jpg" image_file.save(temp_jpg_file) t2 = time.time() - t1 print(t2) result = darknet.detect(yolov3_net,yolov3_meta,temp_jpg_file,thresh=.65) return_json = {"sequence":image_seq,"result":result} #print(return_json) t2 = time.time() - t1 print(t2) return flask.jsonify(return_json) else: error_json = {} error_json["Error"] = "Image Error" return flask.jsonify(error_json)
def predict(self, img_list=[]): '''Detect and localize features in an image Inputs ------ img_list: list images to be analyzed thresh: float threshold confidence for detection Outputs ------- predictions: list list of dicts n images => n lists of dicts per holo prediction: {'conf': 50%, 'bbox': (x_centroid, y_centroid, width, height)} ''' predictions = [] for image in img_list: yolopred = darknet.detect(self.net, self.meta, image, self.threshold, self.nms) imagepreds = [] for pred in yolopred: (label, conf, bbox) = pred imagepreds.append({'label': label, 'conf': conf, 'bbox': bbox}) predictions.append(imagepreds) return predictions
# Stupid python path shit. # Instead just add darknet.py to somewhere in your python path # OK actually that might not be a great idea, idk, work in progress # Use at your own risk. or don't, i don't care import sys, os sys.path.append(os.path.join(os.getcwd(),'python/')) import darknet as dn import pdb dn.set_gpu(0) net = dn.load_net("cfg/yolo-thor.cfg", "/home/pjreddie/backup/yolo-thor_final.weights", 0) meta = dn.load_meta("cfg/thor.data") r = dn.detect(net, meta, "data/bedroom.jpg") print r # And then down here you could detect a lot more images like: r = dn.detect(net, meta, "data/eagle.jpg") print r r = dn.detect(net, meta, "data/giraffe.jpg") print r r = dn.detect(net, meta, "data/horses.jpg") print r r = dn.detect(net, meta, "data/person.jpg") print r
for i in range(meta.classes): if probs[j][i] > 0: res.append((meta.names[i], probs[j][i], (boxes[j].x, boxes[j].y, boxes[j].w, boxes[j].h))) res = sorted(res, key=lambda x: -x[1]) dn.free_ptrs(dn.cast(probs, dn.POINTER(dn.c_void_p)), num) return res import sys, os sys.path.append(os.path.join(os.getcwd(),'python/')) import darknet as dn # Darknet net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0) meta = dn.load_meta("cfg/coco.data") r = dn.detect(net, meta, "data/dog.jpg") print r # scipy arr= imread('data/dog.jpg') im = array_to_image(arr) r = detect2(net, meta, im) print r # OpenCV arr = cv2.imread('data/dog.jpg') im = array_to_image(arr) dn.rgbgr_image(im) r = detect2(net, meta, im) print r