Unverified Commit ee3673ab authored by mikebitsoko's avatar mikebitsoko Committed by GitHub

Update

parent 37822458
"""
BITSOKO SMART INVENTORY AI CONTROLLER
YoloOneTouch - Tested on Ubuntu 16.04LTS, Python3 - @Bitsoko
---- . _ _ _
| | | |
......@@ -26,12 +27,19 @@
$pwd
"""
import os
import glob
import xml.etree.ElementTree as ET
from PIL import Image
import shutil
import sys
import urllib.request
darknet_path = '/home/bitsoko/darknet'
image_path = 'images'
image_path = 'plates'
# Directory where the data will reside, relative to './darknet'
#also serves as your custom model name
output_path = 'bitsoko_model'
output_path = 'plates_model'
#test size percentage
percentage_test = 10
#your gpus, i have three
......@@ -42,27 +50,31 @@ weights_url = "https://pjreddie.com/media/files/darknet19_448.conv.23"
yolo_cfg = 'yolo-obj.cfg'
import os
import glob
import xml.etree.ElementTree as ET
from PIL import Image
import shutil
import sys
import urllib.request
RUNTIME_STATUS = 'Idle'
LABELS_COUNT = 0
DATA_COUNT = 0
MODEL_NAME = output_path
def update_status(status):
global RUNTIME_STATUS
RUNTIME_STATUS = status
def yolo_model_installed():
if not os.path.exists(darknet_path):
print('Yolo Darknet not installed!')
update_status('Yolo Darknet not installed!')
sys.exit()
def weights_check_or_download():
if not os.path.exists(darknet_path+'/'+weights):
print('Weights not found,Downloading...')
update_status('Weights not found,Downloading...')
file_name = weights_url.split('/')[-1]
u = urllib.request.urlopen(weights_url)
f = open(darknet_path+'/'+file_name, 'wb')
file_size = int(u.headers["Content-Length"])
print ("Downloading: %s MBs: %s" % (file_name, file_size/1000000))
update_status ("Downloading: %s MBs: %s" % (file_name, file_size/1000000))
file_size_dl = 0
block_sz = 8192
while True:
......@@ -78,9 +90,10 @@ def weights_check_or_download():
f.close()
def confirm_images_path():
if not os.path.exists(image_path):
print('Images directory not found!')
update_status('Images directory not found!')
sys.exit()
def confirm_output_path():
......@@ -103,6 +116,8 @@ def xml_reader(path):
int(member[4][3].text)
)
xml_list.append(value)
global DATA_COUNT
DATA_COUNT = len(xml_list)
return xml_list
#yolo bboxes format
......@@ -128,19 +143,21 @@ def unique_labels(xml_list):
for line in xml_list:
if line[3] not in unique_classes and line[3] != 'class':
unique_classes.append(line[3])
global LABELS_COUNT
LABELS_COUNT = len(unique_classes)
return unique_classes
def to_yolo(xml_list,unique_classes):
for line in xml_list:
filename,width,height,_class,xmin,ymin,xmax,ymax = line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7]
print(filename,width,height,_class,xmin,ymin,xmax,ymax)
# print(filename,width,height,_class,xmin,ymin,xmax,ymax)
shutil.copy(image_path+'/'+filename, output_path+'/')
im = Image.open(image_path+'/'+filename)
w = int(im.size[0])
h = int(im.size[1])
b = (float(xmin), float(xmax), float(ymin), float(ymax))
bb = convert((w,h),b)
print(bb)
# print(bb)
save_data(filename.split('.')[0],unique_classes.index(_class),bb)
......@@ -185,13 +202,24 @@ def move_to_darknet():
shutil.move(output_path, darknet_path)
def launch_training():
update_status('Training...')
command = "./darknet detector train "+output_path+"/"+output_path+".data"+" "+output_path+"/"+output_path+".cfg"+" "+weights+" -gpus "+gpus
os.system("gnome-terminal -e 'bash -c \"cd "+darknet_path+" && "+command+" ; exec bash\"'")
def resume_training():
update_status('Resumed training...')
command = "./darknet detector train "+output_path+"/"+output_path+".data"+" "+output_path+"/"+output_path+".cfg"+" backup/"+output_path+".backup -gpus "+gpus
os.system("gnome-terminal -e 'bash -c \"cd "+darknet_path+" && "+command+" ; exec bash\"'")
def reload_classes():
pass
def launch_testing():
print('Testing command: ',"cd "+darknet_path+" && "+"./darknet detector train "+output_path+"/"+output_path+".cfg"+" <weights>")
print("Weights file(s) found in ",darknet_path+"/backup")
def main():
yolo_model_installed()
weights_check_or_download()
......
from ctypes import *
import math
import random
from flask import Flask, jsonify, request
from flask_cors import CORS
from flask_cors import CORS
import os
from math import floor
def sample(probs):
s = sum(probs)
probs = [a/s for a in probs]
r = random.uniform(0, 1)
for i in range(len(probs)):
r = r - probs[i]
if r <= 0:
return i
return len(probs)-1
def c_array(ctype, values):
arr = (ctype*len(values))()
arr[:] = values
return arr
class BOX(Structure):
_fields_ = [("x", c_float),
("y", c_float),
("w", c_float),
("h", c_float)]
class IMAGE(Structure):
_fields_ = [("w", c_int),
("h", c_int),
("c", c_int),
("data", POINTER(c_float))]
class METADATA(Structure):
_fields_ = [("classes", c_int),
("names", POINTER(c_char_p))]
#lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL)
lib = CDLL("/home/bitsoko/darknet/libdarknet.so", RTLD_GLOBAL)
lib.network_width.argtypes = [c_void_p]
lib.network_width.restype = c_int
lib.network_height.argtypes = [c_void_p]
lib.network_height.restype = c_int
predict = lib.network_predict
predict.argtypes = [c_void_p, POINTER(c_float)]
predict.restype = POINTER(c_float)
set_gpu = lib.cuda_set_device
set_gpu.argtypes = [c_int]
make_image = lib.make_image
make_image.argtypes = [c_int, c_int, c_int]
make_image.restype = IMAGE
make_boxes = lib.make_boxes
make_boxes.argtypes = [c_void_p]
make_boxes.restype = POINTER(BOX)
free_ptrs = lib.free_ptrs
free_ptrs.argtypes = [POINTER(c_void_p), c_int]
num_boxes = lib.num_boxes
num_boxes.argtypes = [c_void_p]
num_boxes.restype = c_int
make_probs = lib.make_probs
make_probs.argtypes = [c_void_p]
make_probs.restype = POINTER(POINTER(c_float))
detect = lib.network_predict
detect.argtypes = [c_void_p, IMAGE, c_float, c_float, c_float, POINTER(BOX), POINTER(POINTER(c_float))]
reset_rnn = lib.reset_rnn
reset_rnn.argtypes = [c_void_p]
load_net = lib.load_network
load_net.argtypes = [c_char_p, c_char_p, c_int]
load_net.restype = c_void_p
free_image = lib.free_image
free_image.argtypes = [IMAGE]
letterbox_image = lib.letterbox_image
letterbox_image.argtypes = [IMAGE, c_int, c_int]
letterbox_image.restype = IMAGE
load_meta = lib.get_metadata
lib.get_metadata.argtypes = [c_char_p]
lib.get_metadata.restype = METADATA
load_image = lib.load_image_color
load_image.argtypes = [c_char_p, c_int, c_int]
load_image.restype = IMAGE
rgbgr_image = lib.rgbgr_image
rgbgr_image.argtypes = [IMAGE]
predict_image = lib.network_predict_image
predict_image.argtypes = [c_void_p, IMAGE]
predict_image.restype = POINTER(c_float)
network_detect = lib.network_detect
network_detect.argtypes = [c_void_p, IMAGE, c_float, c_float, c_float, POINTER(BOX), POINTER(POINTER(c_float))]
def classify(net, meta, im):
out = predict_image(net, im)
res = []
for i in range(meta.classes):
res.append((meta.names[i], out[i]))
res = sorted(res, key=lambda x: -x[1])
return res
def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
im = load_image(image, 0, 0)
boxes = make_boxes(net)
probs = make_probs(net)
num = num_boxes(net)
network_detect(net, im, thresh, hier_thresh, nms, boxes, probs)
res = []
for j in range(num):
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])
free_image(im)
free_ptrs(cast(probs, POINTER(c_void_p)), num)
return res
# Instantiate our Node
app = Flask(__name__)
CORS(app)
#instatiate network
net = load_net("cfg/yolo-obj.cfg", "backup/yolo-obj_60000.weights", 0)
meta = load_meta("obj.data")
def floored_percentage(val, digits):
val *= 10 ** (digits + 2)
return '{1:.{0}f}%'.format(digits, floor(val) / 10 ** digits)
@app.route('/detect', methods=['POST'])
def yolo_in():
file = request.files['image']
file.save(os.path.join('data',file.filename))
r = detect(net,meta,os.path.join('data',file.filename))
os.remove(os.path.join('data',file.filename))
data = []
for i in r:
data.append([i[0],i[1]])
response = {
'results':data,
}
return jsonify(response),200
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
from flask import Flask, jsonify, request
from flask_cors import CORS
from flask_cors import CORS
import os
import OneTouchYolo as oty
# Instantiate our Server
app = Flask(__name__)
CORS(app)
@app.route('/status', methods=['GET'])
def status():
response = {
'status':oty.RUNTIME_STATUS,
'model_name':oty.MODEL_NAME,
'classes':oty.LABELS_COUNT,
'data_count':oty.DATA_COUNT,
}
return jsonify(response),200
@app.route('/init', methods=['GET'])
def init():
oty.main()
response = {
'model':'Started....',
}
return jsonify(response),200
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment