|
import cv2 |
|
import numpy as np |
|
import time |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
INPUT_WIDTH = 320 |
|
INPUT_HEIGHT = 320 |
|
SCORE_THRESHOLD = 0.45 |
|
NMS_THRESHOLD = 0.45 |
|
CONFIDENCE_THRESHOLD = 0.5 |
|
|
|
|
|
FONT_FACE = cv2.FONT_HERSHEY_SIMPLEX |
|
FONT_SCALE = 0.7 |
|
THICKNESS = 1 |
|
|
|
|
|
BLACK = (0,0,0) |
|
BLUE = (255,178,50) |
|
YELLOW = (0,255,255) |
|
classesFile = "coco.names" |
|
classes = None |
|
|
|
|
|
ch_detection_modelWeights = "best_upwork.onnx" |
|
ch_detection_model = cv2.dnn.readNet(ch_detection_modelWeights) |
|
x_=["-","0","1","2","3","4","5","6","7","8","9"] |
|
|
|
|
|
|
|
|
|
|
|
def draw_label(im, label, x, y): |
|
"""Draw text onto image at location.""" |
|
|
|
text_size = cv2.getTextSize(label, FONT_FACE, FONT_SCALE, THICKNESS) |
|
dim, baseline = text_size[0], text_size[1] |
|
|
|
cv2.rectangle(im, (x,y), (x + dim[0], y + dim[1] + baseline), (0,0,0), cv2.FILLED); |
|
|
|
cv2.putText(im, label, (x, y + dim[1]), FONT_FACE, FONT_SCALE, YELLOW, THICKNESS, cv2.LINE_AA) |
|
def pre_process(input_image, net,w,h): |
|
|
|
|
|
blob = cv2.dnn.blobFromImage(input_image, 1/255, (w, h), [0,0,0], 1, crop=False) |
|
|
|
|
|
net.setInput(blob) |
|
|
|
|
|
outputs = net.forward(net.getUnconnectedOutLayersNames()) |
|
return outputs |
|
|
|
def get_xyxy(input_image,image_height,image_width, outputs,w,h): |
|
|
|
class_ids = [] |
|
confidences = [] |
|
boxes = [] |
|
output_boxes=[] |
|
results_cls_id=[] |
|
|
|
rows = outputs[0].shape[1] |
|
|
|
x_factor = image_width / w |
|
y_factor = image_height / h |
|
|
|
for r in range(rows): |
|
row = outputs[0][0][r] |
|
confidence = row[4] |
|
|
|
if confidence >= CONFIDENCE_THRESHOLD: |
|
classes_scores = row[5:] |
|
|
|
class_id = np.argmax(classes_scores) |
|
|
|
if (classes_scores[class_id] > SCORE_THRESHOLD): |
|
confidences.append(confidence) |
|
class_ids.append(class_id) |
|
cx, cy, w, h = row[0], row[1], row[2], row[3] |
|
left = int((cx - w/2) * x_factor) |
|
top = int((cy - h/2) * y_factor) |
|
width = int(w * x_factor) |
|
height = int(h * y_factor) |
|
box = np.array([left, top, width, height,]) |
|
boxes.append(box) |
|
|
|
indices = cv2.dnn.NMSBoxes(boxes, confidences, CONFIDENCE_THRESHOLD, NMS_THRESHOLD) |
|
for i in indices: |
|
box = boxes[i] |
|
left = box[0] |
|
top = box[1] |
|
width = box[2] |
|
height = box[3] |
|
results_cls_id.append(class_ids[i]) |
|
|
|
cv2.rectangle(input_image, (left, top), (left + width, top + height), BLUE, 1) |
|
|
|
boxes[i][2]=left + width |
|
boxes[i][3]=top + height |
|
|
|
output_boxes.append(boxes[i]) |
|
cv2.imwrite('x1.jpg',input_image) |
|
return output_boxes,results_cls_id |
|
|
|
def char_det(input_image,ch_detection_model,w,h): |
|
|
|
detections = pre_process(input_image.copy(), ch_detection_model,w,h) |
|
image_height=input_image.shape[0] |
|
image_width=input_image.shape[1] |
|
bounding_boxes=get_xyxy(input_image,image_height,image_width, detections,w,h) |
|
|
|
|
|
|
|
|
|
|
|
return bounding_boxes |
|
|
|
def rearange_(array_pred,results_cls_id): |
|
scores='' |
|
|
|
ind=np.argsort(array_pred[:,0]) |
|
|
|
|
|
for indx in (ind): |
|
scores=scores+x_[results_cls_id[indx]] |
|
|
|
|
|
return scores |
|
|
|
|
|
def main_func(img,): |
|
scores=0 |
|
t1=time.time() |
|
img = np.array(img) |
|
im2=img.copy() |
|
|
|
|
|
|
|
width_height_diff=img.shape[1]-img.shape[0] |
|
|
|
if width_height_diff>0: |
|
img = cv2.copyMakeBorder(img, 0, width_height_diff, 0, 0, cv2.BORDER_CONSTANT, (0,0,0)) |
|
if width_height_diff<0: |
|
img = cv2.copyMakeBorder(img, 0, 0, 0, int(-1*width_height_diff), cv2.BORDER_CONSTANT, (0,0,0)) |
|
|
|
|
|
cropped_chars_array,results_cls_id=char_det(img.copy(),ch_detection_model,320,320) |
|
if len(cropped_chars_array)!=0: |
|
cropped_chars_array=np.asarray(cropped_chars_array) |
|
scores=rearange_(cropped_chars_array,results_cls_id) |
|
for box in cropped_chars_array: |
|
left,top,width,height=box |
|
cv2.rectangle(im2, (left, top), (width,height), BLUE, 1) |
|
|
|
time_of_process=(time.time()-t1) |
|
|
|
|
|
return scores,im2,time_of_process |
|
|
|
|
|
import gradio as gr |
|
def final_func(): |
|
gr.Interface(fn=main_func, |
|
inputs=gr.Image(), |
|
outputs=[gr.Textbox(lines=1, label="Scores"),gr.Image(label="Image"),gr.Number(label="Time")],examples=["test.png"]).launch() |
|
|
|
if __name__ == "__main__": |
|
final_func() |