检测框坐标
left = int(widthFactor * left)
top = int(heightFactor * top)
right = int(widthFactor * right)
bottom = int(heightFactor * bottom)
检测目标是否是猫
if self.classes[class_id] == ‘cat’:
cat = {‘box’: [left, top, right, bottom]}
output.append(cat)
return output
以上猫检测模型,使用了 MobileNet-SSD
目标检测模型,这里对训练后 MobileNet-SSD
模型架构和模型权重参数文件进行压缩供大家进行下载,也可以自己构建模型训练获得 MobileNet-SSD
模型参数。
接下来将使用 OpenCV
创建一个深度学习猫检测 Web API
,cat_detection
项目对 Web
服务器应用程序实现在 Web
端检测猫,cat_detection.py
脚本负责解析请求并构建对客户端的响应:
cat_detection.py
from flask import Flask, request, jsonify
import urllib.request
from image_processing import ImageProcessing
app = Flask(name)
ip = ImageProcessing()
@app.errorhandler(400)
def bad_request(e):
return jsonify({‘status’: ‘Not ok’, ‘message’: ‘This server could not understand your request’}), 400
@app.errorhandler(404)
def not_found(e):
return jsonify({‘status’: ‘Not found’, ‘message’: ‘Route not found’}), 404
@app.errorhandler(500)
def not_found(e):
return jsonify({‘status’: ‘Internal error’, ‘message’: ‘Internal error occurred in server’}), 500
@app.route(‘/catfacedetection’, methods=[‘GET’, ‘POST’, ‘PUT’])
def detect_cat_faces():
if request.method == ‘GET’:
if request.args.get(‘url’):
with urllib.request.urlopen(request.args.get(‘url’)) as url:
return jsonify({‘status’: ‘Ok’, ‘result’: ip.cat_face_detection(url.read())}), 200
else:
return jsonify({‘status’: ‘Bad request’, ‘message’: ‘Parameter url is not present’}), 400
elif request.method == ‘POST’:
if request.files.get(‘image’):
return jsonify({‘status’: ‘Ok’, ‘result’: ip.cat_face_detection(request.files[‘image’].read())}), 200
else:
return jsonify({‘status’: ‘Bad request’, ‘message’: ‘Parameter image is not present’}), 400
else:
return jsonify({‘status’: ‘Failure’, ‘message’: ‘PUT method not supported for API’}), 405
@app.route(‘/catdetection’, methods=[‘GET’, ‘POST’, ‘PUT’])
def detect_cats():
if request.method == ‘GET’:
if request.args.get(‘url’):
with urllib.request.urlopen(request.args.get(‘url’)) as url:
return jsonify({‘status’: ‘Ok’, ‘result’: ip.cat_detection(url.read())}), 200
else:
return jsonify({‘status’: ‘Bad request’, ‘message’: ‘Parameter url is not present’}), 400
elif request.method == ‘POST’:
if request.files.get(‘image’):
return jsonify({‘status’: ‘Ok’, ‘result’: ip.cat_detection(request.files[‘image’].read())}), 200
else:
return jsonify({‘status’: ‘Bad request’, ‘message’: ‘Parameter image is not present’}), 400
else:
return jsonify({‘status’: ‘Failure’, ‘message’: ‘PUT method not supported for API’}), 405
if name == ‘main’:
app.run(host=‘0.0.0.0’)
如上所示,使用 route()
装饰器将 detect_cat_faces()
函数绑定到 /catfacedetection URL
,并将detect_cats()
函数绑定到 /catdetection URL
,并且使用 jsonify()
函数创建 application/json MIME
类型的给定参数的 JSON
表示,此 API
支持 GET
和 POST
请求,我们还通过使用 errorhandler()
装饰函数来注册错误处理程序。
为了测试此API,编写 request_and_draw_rectangle.py
程序:
request_and_draw_rectangle.py
import cv2
import numpy as np
import requests
from matplotlib import pyplot as plt
def show_img_with_matplotlib(color_img, title, pos):
img_RGB = color_img[:, :, ::-1]
ax = plt.subplot(1, 2, pos)
plt.imshow(img_RGB)
plt.title(title, fontsize=10)
plt.axis(‘off’)
CAT_FACE_DETECTION_REST_API_URL = “http://localhost:5000/catfacedetection”
CAT_DETECTION_REST_API_URL = “http://localhost:5000/catdetection”
IMAGE_PATH = “test_example.png”
加载图像构建有效负载
image = open(IMAGE_PATH, ‘rb’).read()
payload = {‘image’: image}
image_array = np.asarray(bytearray(image), dtype=np.uint8)
img_opencv = cv2.imdecode(image_array, -1)
fig = plt.figure(figsize=(12, 7))
plt.suptitle(“Using cat detection API”, fontsize=14, fontweight=‘bold’)
show_img_with_matplotlib(img_opencv, “source image”, 1)
发送 GET 请求
r = requests.post(CAT_DETECTION_REST_API_URL, files=payload)
解析返回信息
print(“status code: {}”.format(r.status_code))
print(“headers: {}”.format(r.headers))
print(“content: {}”.format(r.json()))
json_data = r.json()
result = json_data[‘result’]
绘制猫脸检测框
for cat in result:
left, top, right, bottom = cat[‘box’]
cv2.rectangle(img_opencv, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.circle(img_opencv, (left, top), 10, (0, 0, 255), -1)
cv2.circle(img_opencv, (right, bottom), 10, (255, 0, 0), -1)
发送 GET 请求
r = requests.post(CAT_FACE_DETECTION_REST_API_URL, files=payload)
解析返回信息
print(“status code: {}”.format(r.status_code))
print(“headers: {}”.format(r.headers))
print(“content: {}”.format(r.json()))
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Python开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注Python)
最后
🍅 硬核资料:关注即可领取PPT模板、简历模板、行业经典书籍PDF。
🍅 技术互助:技术群大佬指点迷津,你的问题可能不是问题,求资源在群里喊一声。
🍅 面试题库:由技术群里的小伙伴们共同投稿,热乎的大厂面试真题,持续更新中。
🍅 知识体系:含编程语言、算法、大数据生态圈组件(Mysql、Hive、Spark、Flink)、数据仓库、Python、前端等等。
一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!
AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算
更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!**](https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0)
AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算