| import cv2
|
| import numpy as np
|
| import glob
|
| import random
|
| import pathlib
|
| import subprocess, os
|
| from PIL import Image
|
|
|
|
|
| CONF_THRES = 0.1
|
| NMS_THRES = 0.1
|
|
|
| INPWIDTH = 32*10
|
| INPHEIGHT = 32*9
|
|
|
|
|
| import cv2
|
| from tkinter import *
|
| from tkinter import messagebox
|
| from tkinter import filedialog
|
| from tkinter.ttk import Button, Style, Progressbar
|
| import time
|
| import os
|
|
|
| folder_selected=""
|
| class_name=""
|
| data_class=""
|
| v_class=""
|
|
|
| def add_class():
|
| data_class = add_class_field.get()
|
| class_name = data_class
|
| if data_class == "":
|
| messagebox.showinfo("Warning!!", "Class cant be empty")
|
| else:
|
| print("===============Class Name================")
|
| print(class_name)
|
| v_class=class_name
|
| main()
|
|
|
|
|
|
|
| def main():
|
| folder_selected = filedialog.askdirectory()
|
| v_class =add_class_field.get()
|
| print("=========================class name===================")
|
| print(v_class)
|
| print(folder_selected)
|
| print( "========================================")
|
|
|
|
|
|
|
|
|
| modelBaseDir = "./yolo"
|
| modelConfiguration = modelBaseDir + "/config/food-dark-yolov3-tiny_3l-v3-2.cfg"
|
| modelWeights = modelBaseDir + "/data/food/weights/food-dark-yolov3-tiny_3l-v3-2_200000.weights"
|
| net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
|
|
|
|
|
|
|
|
|
|
|
| classesFile = modelBaseDir + "/data/food/food-classes.names"
|
| classes = None
|
| with open(classesFile, 'rt') as f:
|
| classes = f.read().rstrip('\n').split('\n')
|
|
|
|
|
|
|
| train_path = glob.glob(
|
| r"" +folder_selected)
|
|
|
| images_path = glob.glob(
|
| r"" +folder_selected+"\*.jpg")
|
| layer_names = net.getLayerNames()
|
|
|
| output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
|
| colors = np.random.uniform(0, 255, size=(len(classes), 3))
|
| for img_path in images_path:
|
|
|
| print(img_path)
|
| img = cv2.imread(img_path)
|
| file_name = img_path.rsplit('\\', 1)[1]
|
| file_name = file_name.rsplit('.', 1)[0]
|
| print (file_name)
|
| img = cv2.resize(img, None, fx=0.7, fy=0.7)
|
| height, width, channels = img.shape
|
|
|
|
|
|
|
| blob = cv2.dnn.blobFromImage(img, 0.00392, (INPWIDTH, INPHEIGHT), (0, 0, 0), True, crop=False)
|
|
|
| net.setInput(blob)
|
| outs = net.forward(output_layers)
|
|
|
|
|
| class_ids = []
|
| confidences = []
|
| boxes = []
|
| boxes2 = []
|
| coordinate_list = []
|
|
|
| for out in outs:
|
| for detection in out:
|
| scores = detection[5:]
|
| class_id = np.argmax(scores)
|
| confidence = scores[class_id]
|
| if confidence > CONF_THRES:
|
|
|
|
|
| center_x = int(detection[0] * width)
|
| center_y = int(detection[1] * height)
|
| w = int(detection[2] * width)
|
| h = int(detection[3] * height)
|
| a = class_id
|
| b = detection[0]
|
| c = detection[1]
|
| d = detection[2]
|
| e = detection[3]
|
| print("=====================")
|
| print(a,b,c,d,e,confidence)
|
|
|
|
|
| x = int(center_x - w / 2)
|
| y = int(center_y - h / 2)
|
|
|
| boxes.append([x, y, w, h])
|
| boxes2.append([a, b, c, d,e])
|
| confidences.append(float(confidence))
|
| class_ids.append(class_id)
|
|
|
| coordinate = class_id, detection[0], detection[1], detection[2], detection[3], "Confidence:", confidence
|
|
|
| coordinate_for_txt = class_id, detection[0], detection[1], detection[3], detection[4]
|
| coordinate_list.append(coordinate_for_txt)
|
|
|
| font = cv2.FONT_HERSHEY_PLAIN
|
| indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
|
| filename = os.path.basename(img_path).replace('.jpg', '')
|
| print("========================All Coordinate=====================")
|
|
|
|
|
| with open("result/obj_train_data/%s.txt" %filename, "w") as file:
|
| for i in range(len(boxes)):
|
| if i in indexes:
|
| x, y, w, h = boxes[i]
|
| label = str(classes[class_ids[i]])
|
| color = colors[class_ids[i]]
|
| cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
|
| cv2.putText(img, label, (x, y + 30), font, 3, color, 2)
|
| a, b, c, d, e =boxes2[i]
|
| ab = a, b, c, d, e, confidence
|
| print("=========================Choosen Coordinate=======================")
|
| print(ab)
|
| printout = str(a)+" "+ str(b)+" "+ str(c)+" "+ str(d)+" "+ str(e)+ "\n"
|
| file.write(printout)
|
|
|
| cv2.imshow('result image', img)
|
| cv2.waitKey(0)
|
|
|
| path = folder_selected
|
| listdir = os.listdir(path)
|
|
|
| with open("result/train.txt", "w") as train:
|
| for file in listdir:
|
| train.writelines("data/obj_train_data/"+file+"\n")
|
|
|
| print("Arr", indexes)
|
| print("========================End============================")
|
|
|
|
|
| with open("result/obj.names", "w") as names:
|
| names.write(v_class)
|
|
|
|
|
| with open("result/obj.data", "w") as data:
|
| data_content = "classes = "+ "1" + "\n" + "train = data/train.txt" + "\n" + "names = data/obj.names" + "\n" + "backup = backup/" + "\n"
|
| data.write(data_content)
|
| messagebox.showinfo("Notif", "Process Completed")
|
|
|
| root = Tk()
|
| root.geometry("800x300")
|
| root.title('SVG Auto Annonate v0.99')
|
|
|
| def bar():
|
|
|
| print("============Browse File=============")
|
|
|
|
|
| def execute():
|
| main()
|
|
|
| s = Style()
|
|
|
|
|
|
|
| field_frame = Frame(root)
|
| field_frame.pack(pady=20)
|
|
|
|
|
|
|
|
|
| btn_group_frame = LabelFrame(root, padx=10, pady=10)
|
| btn_group_frame.pack(pady=20)
|
|
|
| execute_info_frame = LabelFrame(root, text="Execution Progress", padx=10, pady=10)
|
| execute_info_frame.pack(pady=20)
|
|
|
| exit_btn_frame = Frame(root)
|
| execute_info_frame.pack(side=BOTTOM)
|
|
|
|
|
|
|
|
|
|
|
|
|
| field_label = Label(field_frame, text="Class")
|
| add_class_field = Entry(field_frame)
|
|
|
|
|
|
|
| btn_execute = Button(btn_group_frame, text="Browse and Execute", width=25, style='execute_btn.TButton', command=add_class)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| btn_execute.grid(column=0, row=1)
|
|
|
| field_label.grid(column=0, row=2, padx=15)
|
| add_class_field.grid(column=1, row=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
| s.configure('execute_btn.TButton', background='blue')
|
|
|
|
|
|
|
|
|
|
|
|
|
| root.mainloop()
|
| |