阅读 204

开源图像识别、imageai图像识别、对象识别、识别人、车、猫、狗等80种 简易版

开源图像识别、imageai图像识别、对象识别、识别人、车、猫、狗等80种 简易版

imageai官网地址: 传送门

详细版地址:https://blog.csdn.net/Baldprogrammer/article/details/116358180


项目地址:传送门

1、安装环境(最终版)

安装python-3.6.8-amd64 一键安装,勾选添加到path中


安装imageAI --官网地址:https://imageai-cn.readthedocs.io/zh_CN/latest/ImageAI.html#id1

cmd执行:

pip3 install --index-url https://pypi.douban.com/simple -U tensorflow==1.4.0 

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U numpy==1.16.0

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U scipy==1.4.1

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U opencv-python==4.5.1.48

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U pillow==8.2.0

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U matplotlib==3.3.4

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U h5py==2.10.0

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U keras==2.2.0

pip3 install https://github.com/OlafenwaMoses/ImageAI/releases/download/2.0.1/imageai-2.0.1-py3-none-any.whl

1

2

3

4

5

6

7

8

9

10

11

模型文件下载地址:传送门

图像预测:

from imageai.Detection import ObjectDetection

import os


# os.environ["TF_CPP_MIN_LOG_LEVEL"]='1' # 这是默认的显示等级,显示所有信息

# os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 warning 和 Error

# os.environ["TF_CPP_MIN_LOG_LEVEL"]='3' # 只显示 Error


execution_path = os.getcwd()

detector = ObjectDetection()

detector.setModelTypeAsRetinaNet()



detector.setModelPath(os.path.join(execution_path+"\\model\\" , "resnet50_coco_best_v2.0.1.h5"))

# 设置模型的检测速度 detection_speed="fast" “normal”(default), “fast”, “faster” , “fastest” and “flash”

detector.loadModel(detection_speed="normal")



# 定义使用的模型

custom_objects = detector.CustomObjects( person=True, bicycle=True, car=True, motorcycle=True, airplane=True,

                      bus=True, train=True, truck=True, boat=True, traffic_light=True, fire_hydrant=True, stop_sign=True,

                      parking_meter=True, bench=True, bird=True, cat=True, dog=True, horse=True, sheep=True, cow=True, elephant=True, bear=True, zebra=True,

                      giraffe=True, backpack=True, umbrella=True, handbag=True, tie=True, suitcase=True, frisbee=True, skis=True, snowboard=True,

                      sports_ball=True, kite=True, baseball_bat=True, baseball_glove=True, skateboard=True, surfboard=True, tennis_racket=True,

                      bottle=True, wine_glass=True, cup=True, fork=True, knife=True, spoon=True, bowl=True, banana=True, apple=True, sandwich=True, orange=True,

                      broccoli=True, carrot=True, hot_dog=True, pizza=True, donot=True, cake=True, chair=True, couch=True, potted_plant=True, bed=True,

                      dining_table=True, toilet=True, tv=True, laptop=True, mouse=True, remote=True, keyboard=True, cell_phone=True, microwave=True,

                      oven=True, toaster=True, sink=True, refrigerator=True, book=True, clock=True, vase=True, scissors=True, teddy_bear=True, hair_dryer=True,

                      toothbrush=True)



# 是否把识别对象抠出来放到文件夹里

isSingle=True


inPath=execution_path+"\\image\\in\\"

outPath=execution_path+"\\image\\out\\"


all_images_array = []

all_files = os.listdir(inPath)

for each_file in all_files:

    if(each_file.endswith(".jpg") or each_file.endswith(".png")):

        all_images_array.append(each_file)



for path in all_images_array:

    # 进行识别

    detections, objects_path = detector.detectCustomObjectsFromImage(


        extract_detected_objects=isSingle,  # 将识别的图片单独保存出来


        minimum_percentage_probability=50,  # 置信度


        input_type="file",  # 输入文件格式 file/array/stream


        output_type="file",  # 输出文件格式 file/array/stream


        custom_objects=custom_objects,  # 选择可用模型


        input_image=os.path.join(inPath, path),  # 输入文件


        output_image_path=os.path.join(outPath, "copy_"+path),  # 输出文件


    )


    # 输出识别信息

    if isSingle:

        for eachObject, eachObjectPath in zip(detections, objects_path):

            print(eachObject["name"] + " : " + eachObject["percentage_probability"])

            print("Object's image saved in " + eachObjectPath)

    else:

        for eachObject in detections:

            print(eachObject["name"] + " : " + eachObject["percentage_probability"])

            print("--------------------------------")







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

对象检测

from imageai.Detection import ObjectDetection

import os


# os.environ["TF_CPP_MIN_LOG_LEVEL"]='1' # 这是默认的显示等级,显示所有信息

# os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 warning 和 Error

# os.environ["TF_CPP_MIN_LOG_LEVEL"]='3' # 只显示 Error


execution_path = os.getcwd()

detector = ObjectDetection()

detector.setModelTypeAsRetinaNet()



detector.setModelPath(os.path.join(execution_path+"\\model\\" , "resnet50_coco_best_v2.0.1.h5"))

# 设置模型的检测速度 detection_speed="fast" “normal”(default), “fast”, “faster” , “fastest” and “flash”

detector.loadModel(detection_speed="normal")



# 定义使用的模型

custom_objects = detector.CustomObjects( person=True, bicycle=True, car=True, motorcycle=True, airplane=True,

                      bus=True, train=True, truck=True, boat=True, traffic_light=True, fire_hydrant=True, stop_sign=True,

                      parking_meter=True, bench=True, bird=True, cat=True, dog=True, horse=True, sheep=True, cow=True, elephant=True, bear=True, zebra=True,

                      giraffe=True, backpack=True, umbrella=True, handbag=True, tie=True, suitcase=True, frisbee=True, skis=True, snowboard=True,

                      sports_ball=True, kite=True, baseball_bat=True, baseball_glove=True, skateboard=True, surfboard=True, tennis_racket=True,

                      bottle=True, wine_glass=True, cup=True, fork=True, knife=True, spoon=True, bowl=True, banana=True, apple=True, sandwich=True, orange=True,

                      broccoli=True, carrot=True, hot_dog=True, pizza=True, donot=True, cake=True, chair=True, couch=True, potted_plant=True, bed=True,

                      dining_table=True, toilet=True, tv=True, laptop=True, mouse=True, remote=True, keyboard=True, cell_phone=True, microwave=True,

                      oven=True, toaster=True, sink=True, refrigerator=True, book=True, clock=True, vase=True, scissors=True, teddy_bear=True, hair_dryer=True,

                      toothbrush=True)



# 是否把识别对象抠出来放到文件夹里

isSingle=True



# 进行识别

detections, objects_path  = detector.detectCustomObjectsFromImage(


                    extract_detected_objects=isSingle, #将识别的图片单独保存出来


                    minimum_percentage_probability=50, #置信度


                    input_type="file", #输入文件格式 file/array/stream


                    output_type="file", #输出文件格式 file/array/stream


                    custom_objects=custom_objects,  #选择可用模型


                    input_image=os.path.join(execution_path+"\\image\\in\\", "13.jpg"), # 输入文件


                    output_image_path=os.path.join(execution_path+"\\image\\out\\", "13(2).jpg"), # 输出文件


                    )




#输出识别信息

if isSingle:

    for eachObject, eachObjectPath in zip(detections, objects_path):

        print(eachObject["name"] + " : " + eachObject["percentage_probability"])

        print("Object's image saved in " + eachObjectPath)

else:

    for eachObject in detections:

        print(eachObject["name"] + " : " + eachObject["percentage_probability"])

        print("--------------------------------")



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

视频检测

from imageai.Detection import VideoObjectDetection

import os


# os.environ["TF_CPP_MIN_LOG_LEVEL"]='1' # 这是默认的显示等级,显示所有信息

# os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 warning 和 Error

# os.environ["TF_CPP_MIN_LOG_LEVEL"]='3' # 只显示 Error


execution_path = os.getcwd()


detector = VideoObjectDetection()

detector.setModelTypeAsRetinaNet()

detector.setModelPath(os.path.join(execution_path+"\\model\\", "resnet50_coco_best_v2.0.1.h5"))

# 模型等级 “normal”(default), “fast”, “faster” , “fastest” and “flash”

detector.loadModel(detection_speed="fastest")




# 定义使用的模型

custom_objects = detector.CustomObjects( person=True, bicycle=True, car=True, motorcycle=True, airplane=True,

                      bus=True, train=True, truck=True, boat=True, traffic_light=True, fire_hydrant=True, stop_sign=True,

                      parking_meter=True, bench=True, bird=True, cat=True, dog=True, horse=True, sheep=True, cow=True, elephant=True, bear=True, zebra=True,

                      giraffe=True, backpack=True, umbrella=True, handbag=True, tie=True, suitcase=True, frisbee=True, skis=True, snowboard=True,

                      sports_ball=True, kite=True, baseball_bat=True, baseball_glove=True, skateboard=True, surfboard=True, tennis_racket=True,

                      bottle=True, wine_glass=True, cup=True, fork=True, knife=True, spoon=True, bowl=True, banana=True, apple=True, sandwich=True, orange=True,

                      broccoli=True, carrot=True, hot_dog=True, pizza=True, donot=True, cake=True, chair=True, couch=True, potted_plant=True, bed=True,

                      dining_table=True, toilet=True, tv=True, laptop=True, mouse=True, remote=True, keyboard=True, cell_phone=True, microwave=True,

                      oven=True, toaster=True, sink=True, refrigerator=True, book=True, clock=True, vase=True, scissors=True, teddy_bear=True, hair_dryer=True,

                      toothbrush=True)



video_path = detector.detectCustomObjectsFromVideo(custom_objects=custom_objects, #指定模型

                                                   input_file_path=os.path.join(execution_path+"\\video\\in\\", "holo1.mp4"),  #输入输入视频路径

                                                   output_file_path=os.path.join(execution_path+"\\video\\out\\", "holo1_1"),  #不指定默认为avi格式

                                                   frames_per_second=20, #输出视频的帧数

                                                   frame_detection_interval=5,#帧间隔 每隔几帧检测一回

                                                   log_progress=True) #输出日志









————————————————

版权声明:本文为CSDN博主「No baldness」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/Baldprogrammer/article/details/116357434


文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐