File size: 11,261 Bytes
d658572
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- encoding: utf-8 -*-
# ---------------------------- Food Classifier ---------------------------
# This code is written to test the food classifier using customized YOLO model
# It supports YOLO v3 and v4 as of 20Dec. 11, 2020
# For options in detail, please refer to /config/confi.py

# Usage example:  python3 food_classifier_yolo.py --video=run.mp4
#                 python3 food_classifier_yolo.py --image=bird.jpg
# -------------------------------------------------------------------------
# modified by speedpointer, 05 Aug, 2025

import os.path
import cv2 as cv
import argparse
import sys
import numpy as np
import json
from PIL import ImageFont, ImageDraw, Image

from config import config

parser = argparse.ArgumentParser(description='Food Classification and Localization ver. 0.9')
parser.add_argument('--image', help='Full path to image file.')
parser.add_argument('--video', help='Full path to video file.')
parser.add_argument('--showText', type=int, default=1, help='show text in the output.')
parser.add_argument('--ps', type=int, default=1, help='stop each image in the screen.')
args = parser.parse_args()

# Initialize the parameters
args.image      = config.TEST_IMAGE_PATH  # image path
args.video      =  config.TEST_VIDEO_PATH # video path
args.showText = config.SHOW_TEXT_FLAG #1
args.ps = config.PS_FLAG # 1

# refine the inferences
confThreshold   = config.CONF_THRES # 0.1 #0.5  # Confidence threshold
nmsThreshold    = config.NMS_THRES #0.1 #0.4  # Non-maximum suppression threshold

# modes inference size regardless of input image size
inpWidth        = config.INPWIDTH # 32*10  # 608     #Width of network's input image # 320(32*10)
inpHeight       = config.INPHEIGHT # 32*9 # 608     #Height of network's input image # 288(32*9) best

# model base directory
modelBaseDir    = config.ModelBaseDir # "C:/Users/mmc/workspace/yolo"

# Load names of classes from a file
classesFile = os.path.sep.join([modelBaseDir, config.CLASSES_FILE])
classes = None
with open(classesFile, 'rt', encoding='utf-8') as f:
    classes = f.read().rstrip('\n').split('\n')

# Load codes of classes from a file
classes_File_Codes = os.path.sep.join([modelBaseDir, config.CLASSES_FILE_CODE])
classes_codes = None
with open(classes_File_Codes, 'rt', encoding='utf-8') as f:
    classes_codes = f.read().rstrip('\n').split('\n')

assert (len(classes) == len(classes_codes))

# model configuration and weights paths
modelConfiguration = os.path.sep.join([modelBaseDir, config.Model_Configuration])
modelWeights = os.path.sep.join([modelBaseDir, config.Model_Weights])

# load a given model
net = cv.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv.dnn.DNN_TARGET_OPENCL_FP16)

# Get the names of the output layers
def getOutputsNames(net):
    # Get the names of all the layers in the network
    layersNames = net.getLayerNames()
    # Get the names of the output layers, i.e. the layers with unconnected outputs
    # Fix for OpenCV 4.x compatibility
    unconnected = net.getUnconnectedOutLayers()
    if len(unconnected.shape) == 1:
        return [layersNames[i - 1] for i in unconnected]
    else:
        return [layersNames[i[0] - 1] for i in unconnected]

# Draw the predicted bounding box
def drawPred(frame, classId, conf, left, top, right, bottom):
    # Draw a bounding box.
    #    cv.rectangle(frame, (left, top), (right, bottom), (255, 178, 50), 3)
    cv.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 3)

    label = '%.2f' % conf

    # Get the label for the class name and its confidence
    if classes:
        assert (classId < len(classes))
        #label = '%s:%s' % (classes[classId], label)
        label = u'%s' % (classes[classId])
        #label = u'%s' % (classId)
        print('label:{}, class_id:{}'.format(label, classId))


    # Display the label at the top of the bounding box
    labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
    top = max(top, labelSize[1])
    if args.showText:
        #cv.rectangle(frame, (left, top - round(1.5 * labelSize[1])), (left + round(1.5 * labelSize[0]), top + baseLine),
        #        (0, 255, 255), cv.FILLED)
        cv.rectangle(frame, (left, top - round(1.5*labelSize[1])), (left + round(1.5*labelSize[0]), top + baseLine),    (0, 255, 255), cv.FILLED)
        cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 2)

        #fontpath = "./font/gulim.ttc"
        #font_ = ImageFont.truetype(fontpath, 14)
        #img_pil = Image.fromarray(frame)
        #draw = ImageDraw.Draw(img_pil)
        #draw.text((left, top), label, font=font_, fill=(0, 0, 0, 0))
        #frame = np.array(img_pil)
        #cv.imshow('pil', frame)


def postprocess(frame, outs, showimg=False):
    frameHeight = frame.shape[0]
    frameWidth = frame.shape[1]

    # Scan through all the bounding boxes output from the network and keep only the
    # ones with high confidence scores. Assign the box's class label as the class with the highest score.
    classIds = []
    confidences = []
    boxes = []
    for out in outs:
        if(args.showText):
            print("out.shape : ", out.shape)
        for detection in out:
            # if detection[4]>0.001:
            scores = detection[5:]
            classId = np.argmax(scores)
            # if scores[classId]>confThreshold:
            confidence = scores[classId]
            if detection[4] >= confThreshold:
                if(args.showText):
                    print('obj score: ', detection[4], " - confidence:", scores[classId], " - thres : ", confThreshold)
                    #print(detection)
            if confidence >= confThreshold:
                center_x = int(detection[0] * frameWidth)
                center_y = int(detection[1] * frameHeight)
                width = int(detection[2] * frameWidth)
                height = int(detection[3] * frameHeight)
                left = int(center_x - width / 2)
                top = int(center_y - height / 2)
                classIds.append(classId)
                confidences.append(float(confidence))
                boxes.append([left, top, width, height])
                # cv.rectangle(frame, (left, top), (left+width, top+height), (255, 0, 255),2)
                # cv.imshow('test', frame)
                # cv.waitKey(1)

    # Perform non maximum suppression to eliminate redundant overlapping boxes with
    # lower confidences.
    indices = cv.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)
    rests =[]
    for i in indices:
        # Fix for OpenCV 4.x compatibility
        idx = i[0] if isinstance(i, (list, tuple, np.ndarray)) and len(i) > 0 else i
        box = boxes[idx]
        left = box[0]
        top = box[1]
        width = box[2]
        height = box[3]
        rests.append([classIds[idx], left, top, width, height, frameWidth, frameHeight])
        if(showimg):
            drawPred(frame, classIds[idx], confidences[idx], left, top, left + width, top + height)

    return rests

def food_classifier_Json(image):
    # do somthing
    print(args.showText)
    locations = food_classifier_pipeline(frame=image) #[(2321, 0, 0, 10, 10)] # list of (id, rect) from classfication
    jsons = []
    for j,location in enumerate(locations):
        class_id, x, y, width, height, framewidth, frameheight =location
        res_json = {}
        res_json["ClassID"] = classes_codes[class_id] # code , class_id (training class)
        res_json["ClassName"] = classes[class_id]
        res_json["x"] = int(x)
        res_json["y"] = int(y)
        res_json["w"] = int(width)
        res_json["h"] = int(height)
        res_json["framewidth"] = int(framewidth)
        res_json["frameheight"]= int(frameheight)
        jsons.append(res_json)
    print(json.dumps(jsons,ensure_ascii=False))

    return json.dumps(jsons,ensure_ascii=False)

def food_classifier_pipeline(frame):

    # Create a 4D blob from a frame.
    blob = cv.dnn.blobFromImage(frame, 1 / 255, (inpWidth, inpHeight), [0, 0, 0], 1, crop=False)
    # Sets the input to the network
    net.setInput(blob)
    # Runs the forward pass to get output of the output layers
    outs = net.forward(getOutputsNames(net))
    final_infos = postprocess(frame, outs)

    return final_infos

# Process inputs
def main(main_args):
    winName = 'Food Classification Results'
    #cv.namedWindow(winName, cv.WINDOW_AUTOSIZE)
    m_startFrame = np.maximum(0, config.Video_Start_Frame)

    outputFile = "yolo_out_py.avi"
    if (main_args.image):
        # Open the image file
        if not os.path.isfile(main_args.image):
            print("Input image file ", main_args.image, " doesn't exist")
            sys.exit(1)
        cap = cv.VideoCapture(main_args.image)
        outputFile = args.image[:-4] + '_yolo_out_py.jpg'
    elif (main_args.video):
        # Open the video file
        if not os.path.isfile(main_args.video):
            print("Input video file ", main_args.video, " doesn't exist")
            sys.exit(1)
        cap = cv.VideoCapture(main_args.video)
        cap.set(cv.CAP_PROP_POS_FRAMES, m_startFrame)
        outputFile = main_args.video[:-4] + '_yolo_out_py.avi'
    else:
        # Webcam input
        cap = cv.VideoCapture(0)

    # Get the video writer initialized to save the output video
    if (not main_args.image):
        vid_writer = cv.VideoWriter(outputFile, cv.VideoWriter_fourcc('M', 'J', 'P', 'G'), 30,
                                    (round(cap.get(cv.CAP_PROP_FRAME_WIDTH)), round(cap.get(cv.CAP_PROP_FRAME_HEIGHT))))
    pcontinue = True
    while pcontinue:

        # get frame from the video
        hasFrame, frame = cap.read()

        # Stop the program if reached end of video
        if not hasFrame:
            print("Done processing !!!")
            print("Output file is stored as ", outputFile)
            # if(main_args.ps):
            #    cv.waitKey(0)
            #else:
            #    cv.waitKey(1)

            #break

        # Create a 4D blob from a frame.
        blob = cv.dnn.blobFromImage(frame, 1 / 255, (inpWidth, inpHeight), [0, 0, 0], 1, crop=False)
        # Sets the input to the network
        net.setInput(blob)
        # Runs the forward pass to get output of the output layers
        outs = net.forward(getOutputsNames(net))
        if main_args.showText:
            print(getOutputsNames(net))

        postprocess(frame, outs, showimg=True)

        # Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
        if main_args.showText:
            t, _ = net.getPerfProfile()
            label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())
            print(label)
            cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))

        # Write the frame with the detection boxes
        if (main_args.image):
            cv.imwrite(outputFile, frame.astype(np.uint8));
        else:
            vid_writer.write(frame.astype(np.uint8))

        #cv.imshow(winName, frame)
        #cv.waitKey(1)
        pcontinue=False

if __name__ == "__main__":
    main(main_args=args)