Home>

※ Python beginner Please forgive me
※ OS is Windows 10

Keras-Yolo3
https://github.com/qqwwweee/keras-yolo3
There is a library of
Some of them have a Python code called Yolo_video.py and Yolo.py.

Place a learned model in the model_data folder and on the terminal

Python yolo_video.py --input image file name .jpg
When it is input like

, it is such that a book is detected, for example, from the input image.
(I confirmed that it works properly on your PC)

YOLO.PY source code is here.

# -* -coding: UTF-8-* -
"" "
Class Definition of Yolo_v3 Style Detection Model on Image and Video ON: AND VIDEO OF IN MOASS DEFINITIONITION OF YOLO_V3 STYLE DETECTION
"" "
IMPORT COLORSYS
IMPORT OS
From TimeIt Import Default_Timer As Timer
IMPORT NUMPY AS NP
From Keras Import Backend AS K
From keras.models Import Load_Model
From keras.layers Import Input
From Pil Import Image, ImageFont, Imagedraw
From Yolo3.Model Import YOLO_EVAL, YOLO_BODY, TINY_YOLO_BODY
From Yolo3.Utils Import Letterbox_Image
IMPORT OS
From keras.utils import Multi_GPU_Model
Class Yolo (Object):
    _DEFAULTS= {
        "model_path": 'model_data /yolo.h5',
        "anchors_path": 'model_data /yolo_anchors.txt',
        "Classes_Path": 'model_data /coco_classes.txt',
        "Score": 0.3,
        "IOU": 0.45,
        "model_image_size": (416, 416),
        "GPU_NUM": 1,
    }
    @classmethod
    DEF GET_DEFAULTS (CLS, N):
        if n in cls ._defaults:
            Return CLS._Defaults [n]
        else:
            Return "Unrecognized Attribute Name '" + N + "'"
    DEF __INIT __ (self, ** kwargs):
        self .__ dict __. Update (self ._defaults) # set up Default Values
        self .__ dict __. Update (kwargs) # and update with user overrides
        self.Class_Names= self ._get_class ()
        Self.Angels= Self._get_anchors ()
        Self.Sess= K.get_Session ()
        Self.Boxes, Self.SCORES, SELF.CLASSES= Self.Generate ()
    def _get_class (Self):
        classes_path= os.path.expanduser (self.classes_path)
        with Open (Classes_Path) AS F:
            class_names= f.readlines ()
        Class_Names= [C.STRIP () for C in class_Names]
        RETURN CLASS_NAMES
    DEF _get_anchors (Self):
        anchors_path= os.path.expanduser (self.anchors_path)
        with Open (Anchors_Path) AS F:
            anchors= f.readline ()
        anchors= [FLOAT (X) for X in Anchors.Split (',')]
        RETURN NP.Array (Anchors) .Reshape (-1, 2)
    DEF Generate (Self):
        model_path= os.path.expanduser (self.model_path)
        Assert Model_Path.endswith ('. H5'), '. H5'), 'Keras Model or Weights Must Be a. H5 File.'
        # Load Model, Or Construct Model and Load Weights.
        NUM_ANCHORS= LEN (self .Anchors)
        num_classes= len (self.Class_Names)
        IS_TINY_VERSION= NUM_ANCHORS== 6 # DEFAULT SETTING
        Try:
            Self.YOLO_MODEL= LOAD_MODEL (Model_Path, Compile= False)
        EXCEPT:
            Self.YOLO_MODEL= Tiny_YOLO_BODY (INPUT (SHAPE= (NONE, NONE, 3)), NUM_ANCHORS //2, NUM_CLASSES) \
                IF IS_TINY_VERSION ELSE YOLO_BODY (INPUT (SHAPE= (NONE, NONE, 3)), NUM_ANCHORS //3, NUM_CLASSES)
            Self.YOLO_MODEL.LOAD_WEIGHTS (Self.Model_Path) # Make Sure Model, Anchors and Classes Match
        else:
            Assert Self.YOLO_MODEL.LAYERS [-1] .OUTPUT_SHAPE [-1]== \
                NUM_ANCHORS /LEN (self. YOLO_MODEL.OUTPUT) * (NUM_CLASSES + 5), \
                'Mismatch Between Model and Given Anchor and Class SIZES'.) MISMATCH BETWEEN MODEL AND GIVEN ANCHOR AND CLASS SIZES
        Print ('{} model, anchors, and classes loaded. FORMAT (MODEL_PATH))
        # Generate Colors for Drawing Bounding BOXES.
        HSV_TUPLES= [(X /Len (Self.Class_Names), 1., 1.)
                      for X in Range (len (self.class_names))]
        self.colors= list (Map (Lambda x: colorSys.hsv_to_rgb (* X), ​​HSV_TUPLES))
        Self.Colors= LIST
            Map (Lambda x: (int (x [0] * 255), int (x [1] * 255), INT (x [2] * 255)),
                Self.Colors))np.random.seed (10101) # Fixed Seed for Consistent Colors Across Runs.
        np.random.seed (NONE) # Reset Seed to DEFAULT.
        # Generate Output Tensor Targets for Filtered Bounding BOXES.
        self.input_image_shape= k.PlaceHolder (Shape= (2,))
        IF self. GPU_NUM >
= 2:
            Self.YOLO_MODEL= MULTI_GPU_MODEL (SELF.YOLO_MODEL, GPUS= self. GPU_NUM)
        Boxes, SCORES, CLASSES= YOLO_EVAL (Self.Yolo_Model.Output, Self.Angels,
                LEN (self.Class_Names), Self.input_image_Shape,
                score_threshold= self.score, IOU_THRESHOLD= self.iou)
        Return Boxes, Scores, Classes
    Def Detect_Image (Self, Image):
        START= TIMER ()
        IF self .Model_image_Size!= (None, None):
            Assert Self.model_image_size [0]% 32== 0, 'Multiples of 32 Required'
            Assert Self.Model_image_Size [1]% 32== 0, 'Multiples of 32 Required'
            boxed_image= letterbox_image (image, tuple (repersed (self.model_image_size)))
        else:
            NEW_IMAGE_SIZE= (Image.Width-(Image.Width% 32),
                              Image.Height-(Image.Height% 32)
            boxed_image= letterbox_image (image, new_image_size)
        image_data= np.Array (boxed_image, dtype= 'float32')
        Print (image_data.shape)
        image_data /= 255.
        image_data= np.expand_dims (image_data, 0) # Add Batch Dimension.
        OUT_BOXES, OUT_SCORES, OUT_CLASSES= self .Sess.run
            [Self.boxes, Self.scores, Self.Classes],
            FEED_DICT= {
                Self.YOLO_MODEL.INPUT: Image_Data,
                self.input_image_shape: [image.size [1],
 Image.Size [0]],
                K. Larning_phase (): 0
            })
        print ('FOUND {} boxes for {}'. Format (Len (out_boxes), 'img'))
        font= imagefont.trueType (font= 'font /firamono-medium.otf',
                    SIZE= NP.FLOOR (3E-2 * Image.Size [1] + 0.5) .aster ('int32'))
        Thickness= (Image.Size [0] + Image.Size [1]) //300
        for i, c in Reversed (List (ENUMERATE (OUT_CLASSES)):
            predicted_class= self.Class_Names [C]
            BOX= OUT_BOXES [I]
            score= out_scores [i]
            Label= '{} {: .2f}'. Format (predicted_class, score)
            Draw= ImageDraw.Draw (Image)
            label_size= Draw.TextSize (Label, Font)
            TOP, LEFT, BOTTOM, RIGHT= BOX
            TOP= MAX (0, NP.FLOOR (TOP + 0.5) .aster ('int32'))
            Left= max (0, np.floor (Left + 0.5) .aster ('int32'))
            BOTTOM= min (Image.Size [1],
 np.floor (Bottom + 0.5) .aster ('int32'))
            Right= min (image.size [0],
 np.floor (Right + 0.5) .asterPe ('int32'))
            Print (Label, (Left, Top), (Right, Bottom))
            IF TOP-LABEL_SIZE [1] >
= 0:
                TEXT_ORIGIN= NP.Array ([LEFT, TOP-LABEL_SIZE [1]])
            else:
                TEXT_ORIGIN= NP.Array ([LEFT, TOP + 1])
            # My kingdom for a Good Redistributable Image Drawing Library.
            for I in Range (Thickness):
                Draw.Rectangle
                    [LEFT + I, TOP + I, RIGHT-I, BOTTOM-I],
                    Outline= Self.Colors [c])
            Draw.Rectangle
                [TUPLE (TEXT_ORIGIN), TUPLE (TEXT_ORIGIN + LABEL_SIZE)],
                fill= self.colors [c])
            draw.text (Text_Origin, Label, Fill= (0, 0, 0), font= font)
            del Draw
        END= TIMER ()
        Print (end-start)
        Return Image
    DEF CLOSE_SESSION (SELF):
        Self.Sess.Close ()
Def Detect_video (Yolo, Video_path, Output_path= "):
    IMPORT CV2
    VID= CV2.Videocapture (Video_Path)
    IF NOT VID.ISOPENED ():
        RAISE IOERROR ("COULDN'T OPEN WebCAM or Video")
    video_fourcc= int (vid.get (CV2.CAP_PROP_FOURCC))
    Video_FPS= Vid.get (CV2.CAP_PROP_FPS)
    Video_Size= (INT (Vid.get (CV2.CAP_PROP_FRAME_WIDTH)),
                        INT (Vid.get (CV2.CAP_PROP_FRAME_HEIGHT)))
    isoutput= true if output_path!= "" else False
    IF isoutput:
        print ("!!! type:", type (output_path), type (video_fourcc), type (video_fps), type (video_size))OUT= CV2.VideoWriter (OUTPUT_PATH, VIDEO_FOURCC, VIDEO_FPS, VIDEO_SIZE) ACCUM_TIME= 0
    CURR_FPS= 0
    FPS= "FPS:??"
    Prev_Time= Timer ()
    While True:
        RETURN_VALUE, Frame= Vid.Read ()
        image= image.fromArray (Frame)
        Image= YOLO.DETECT_IMAGE (Image)
        Result= NP.Sarray (Image)
        CURR_TIME= TIMER ()
        EXEC_TIME= CURR_TIME-PREV_TIME
        PREV_TIME= CURR_TIME
        ACCUM_TIME= ACCUM_TIME + EXEC_TIME
        CURR_FPS= CURR_FPS + 1
        if accum_time >
 1:
            ACCUM_TIME= ACCUM_TIME-1
            FPS= "FPS:" + STR (CURR_FPS)
            CURR_FPS= 0
        cv2.putText (result, text= fps, org= (3, 15), fontface= cv2.font_hershey_simplex,
                    FONTSCALE= 0.50, color= (255, 0, 0), Thickness= 2)
        cv2.namedwindow ("result", cv2.window_normal)
        CV2.imshow ("Result", Result)
        IF isoutput:
            Out.Write (Result)
        IF CV2.WAITKEY (1) &
 0xFF== ORD ('q'):
            break
    YOLO.CLOSE_SESSION ()

YOLO_VIDEO.PY source code is here.

Import SYS
IMPORT ARGPARSE
From Yolo Import Yolo, detect_video
From PIL Import Image
Def Detect_img (Yolo):
    While True:
        img= Input ('INPUT Image Filename:')
        Try:
            image= image.open (img)
        EXCEPT:
            Print ('Open Error! Try Again!')
            CONTINUE
        else:
            R_IMAGE= YOLO.DETECT_IMAGE (Image)
            R_IMAGE.SHOW ()
    YOLO.CLOSE_SESSION ()
Flags= none
IF __NAME__== '__MAIN__':
    # Class Yolo Defines The Default Value, So Supress Any Default Here
    parser= argparse.argumentParser (argument_default= argparse.suppress)
    '' '
    Command Line Options
    '' '
    parser.add_Argument
        '-Model', Type= STR,
        help= 'path to model weight file, default' + yolo.get_defaults ("model_path")
    )
    parser.add_Argument
        '--anchors', Type= STR,
        Help= 'Path to Anchor Definitions, Default' + YOLO.GET_DEFAULTS ("ANCHORS_PATH")
    )
    parser.add_Argument
        '--classes', Type= STR,
        help= 'path to class definitions, default' + yolo.get_defaults ("Classes_Path")
    )
    parser.add_Argument
        '--gpu_num', Type= int,
        help= 'Number of GPU to Use, Default' + STR (YOLO.GET_DEFAULTS ("GPU_NUM"))
    )
    parser.add_Argument
        '--image', Default= False, Action= "store_true",
        help= 'Image Detection Mode, Will Ignore All Positional Arguments
    )
    '' '
    Command Line Positional Arguments-for Video Detection Mode
    '' '
    parser.add_Argument
        "--input", NARGS= '?', Type= STR, Required= false, DEFAULT= '. /PATH2YOUR_VIDEO',
        Help= "Video Input Path"
    )
    parser.add_Argument
        "--output", NARGS= '?', Type= str, default= "",
        help= "[Optional] Video Output Path"
    )
    Flags= parser.parse_args ()
    IF flags.image:
        "" "
        Image Detection Mode, Disregard Any Remaining Command Line Arguments
        "" "
        Print ("Image Detection Mode")
        IF "INPUT" in flags:
            Print ("Ignoring Remaining Command Line Arguments:" + flags.input + "," + flags.output
        detect_img (Yolo (** VARS (Flags)))
    elif "INPUT" in flags:
        detect_video (yolo (** VARS (Flags)), flags.input, flags.Output)
    else:
        Print ("Must Specify at Least Video_Input_path. See Usage with --help."

YOLO_VIDEO.PY is a mechanism that YOLO.PY is called.

Call yolo.py with your own source code Sample.py,
I want to detect objects in real time (do not use YOLO_VIDEO.PY).
Sample.py's source code is here (the part that does not matter is omitted).

Import SYS
IMPORT ARGPARSE
IMPORT CV2
From Yolo Import Yolo, detect_video
From PIL Import Image
↑ Copy the module imported by YOLO_VIDEO.PY
Image= Camera.get_Image () ← Image taken with the camera is included in Imageimage_size= add_margin (RESCALE (Image), Size) ← The image of Yolo, the image needs to be square, so it is converted to Size × size square image NOSE_DATA= YOLO.DETECT_IMAGE (image_Size_Changed) ← YOLO.PY Call detect_image (), detect nose from person's face, and put the identification of detected areas into NOSE_DATA

created such a program, but the following error occurred at runtime.

nose_data= yolo.detect_image (image_size)
NAMEERROR: NAME 'YOLO' IS NOT DEFINED

YOLO_VIDEO. PY does not know which one is needed.
If you can advise me if you're good?

The worst way is likely to mess with messing up, but can it be able to move the contents of YOLO.PY to SAMPLE.PY?

S.S2021-06-09 05:51:20

NAME 'YOLO' IS NOT DEFINED is displayed as defined, so IMPORT YOLO is required, but it is already as FROM YOLO IMPORT YOLO, DETECT_VIDEO, so I think it is OK YOLO LOLO IMPORT YOLO, DETECT_VIDEO or DETECT_IMAGE. You will. In that case, change to NOSE_DATA= detect_image (image_size) (delete the package name).

metropolis2021-06-09 05:51:20

Thank you, name 'Yolo' IS Not Defined error was solved this way.

S.S2021-06-09 05:51:20