File size: 1,903 Bytes
750920d
 
 
 
c5f58af
750920d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import cv2
from ultralytics import YOLO



classes = ['ain', 'al', 'aleff','bb','dal','dha','dhad','fa','gaaf','ghain','ha','haa','jeem','kaaf','khaa','la','laam',
        'meem','nun','ra','saad','seen','sheen','ta','taa','thaa','thal','toot','waw','ya','yaa','zay']
TargetMapper = dict(zip(range(32),classes))
model = YOLO('best.pt')
def image_inference(image_path):
    print(image_path)
    image = cv2.imread(image_path)
    outputs = model.predict(source=image_path)
    results = outputs[0]
    for i,det in enumerate(results.boxes.xyxy):
        cls = TargetMapper[results.boxes.cls.numpy()[i]]
        #det = results.boxes.xyxy[0]
        cv2.rectangle(
            image,
            (int(det[0]), int(det[1])),
            (int(det[2]), int(det[3])),
            color=(0, 0, 255),
            thickness=2,
            lineType=cv2.LINE_AA
        )
        cv2.putText(image, cls, (int(det[0]), int(det[1])-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 2)

    return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

def video_inference(video_path) :
    cap = cv2.VideoCapture(video_path)
    while(cap.isOpened()):
        ret, frame = cap.read()
        if ret:
            frame_copy = frame.copy()
            outputs = model.predict(source=frame)
            results = outputs[0]#.cpu().numpy()
            for i, det in enumerate(results.boxes.xyxy):
                cls = TargetMapper[results.boxes.cls.numpy()[i]]
                cv2.rectangle(
                    frame_copy,
                    (int(det[0]), int(det[1])),
                    (int(det[2]), int(det[3])),
                    color=(0, 0, 255),
                    thickness=2,
                    lineType=cv2.LINE_AA
                )
                cv2.putText(frame_copy, cls, (int(det[0]), int(det[1])-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 2)
            yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)