提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
现代社会大家对于自己的隐私越来越注重,我们平时在外拍摄风景照片的时候难免会有其他人入镜。在发朋友圈或者微博的时候,为了保证这些人的肖像权我一般都会做马赛克处理。
使用python库加fastapi可以做一个快速处理的webapi,具体介绍如下。
FastAPI 是一个用于构建 API 的现代、快速(高性能)的 web 框架,使用 Python 3.6+ 并基于标准的 Python 类型提示。它具有快速,高效,简单及标准化的特点。
命令如下:
pip install fastapi
pip install "uvicorn[standard]"
代码如下:
from fastapi import HTTPException
import cv2
import os
def do_mosaic(frame, x, y, w, h, neighbor=9):
fh, fw = frame.shape[0], frame.shape[1]
if (y + h > fh) or (x + w > fw):
return
for i in range(0, h - neighbor, neighbor):
for j in range(0, w - neighbor, neighbor):
rect = [j + x, i + y, neighbor, neighbor]
color = frame[i + y][j + x].tolist()
left_up = (rect[0], rect[1])
right_down = (rect[0] + neighbor - 1, rect[1] + neighbor - 1)
cv2.rectangle(frame, left_up, right_down, color, -1)
代码如下:
from fastapi import FastAPI, UploadFile, Form, File, HTTPException, Request
from model import PicItem, PicReq
import time
import os
import mosaic
import logging
import pickle
app = FastAPI()
# setup loggers
logger = logging.getLogger("uvicorn.access")
logger.setLevel(logging.INFO)
handler = logging.handlers.RotatingFileHandler("api.log",mode="a",maxBytes = 100*1024, backupCount = 5)
handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
logger.addHandler(handler)
@app.get("/")
async def hello():
logger.info("enter hello")
logger.error("error test")
return {"message":"hello world!"}
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(), uid: str = Form()):
logger.info("enter create_upload_file uid:%s" % uid)
#check
if uid == "":
logger.error("create_upload_file 未获取到UID")
raise HTTPException(status_code=500, detail="未获取到UID")
if not file:
logger.error("create_upload_file 未获取到文件")
raise HTTPException(status_code=500, detail="未获取到文件")
mime = file.content_type
if mime != "image/jpeg":
logger.error("create_upload_file 只允许图片格式文件")
raise HTTPException(status_code=500, detail="只允许图片格式文件")
#set file save path and name
data = await file.read()
timestr = time.strftime("%Y%m%d%H%M%S",time.localtime())
fname = file.filename.split('.')
folderpath = "files/" + uid
newfname = "%s.%s" % (timestr, fname[1])
try:
if not os.path.exists(folderpath):
os.makedirs(folderpath)
filepath = "%s/%s" % (folderpath, newfname)
with open(filepath,"wb") as f:#save file
f.write(data)
f.close()
except e:
raise HTTPException(status_code=500, detail= e)
#set result body
result = PicItem()
result.uid = uid
result.filename = newfname
#resultstr = pickle.dumps(result).decode('UTF-8','ignore')
#logger.info("return create_upload_file uid:%s response:%s" % (uid, resultstr))
return result
代码如下:
@app.post("/go/")
async def mosaic_pic(picReq: PicReq):
logger.info("enter go request:%s" % picReq)
savedname = mosaic.use_mosaic(picReq.uid, picReq.filename)
logger.info("return go savedname:%s" % savedname)
return savedname
def use_mosaic(uid, fname):
print("enter use_mosaic")
filepath = "files/%s/%s" % (uid, fname)
if not os.path.exists(filepath):
raise HTTPException(status_code=500, detail="文件不存在")
img = cv2.imread(filepath)
fixsize = 128 # declare fix size
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # convert to grey
print("read file success")
# laod opencv schema
classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
color = (0, 255, 0)
# begin to identify face
faceRects = classifier.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=3, minSize=(32, 32))
print(faceRects)
if len(faceRects): # get faces if above zero
print("get faces")
for faceRect in faceRects: # loop each face
x, y, w, h = faceRect
do_mosaic(img, x, y, w, h)
print("finish use_mosaic")
sfname = fname.split(".")
donename = "%s_done.%s" % (sfname[0], sfname[1])
cv2.imwrite("files/%s/%s" % (uid, donename) , img)
return donename
原照片:
处理后的照片:
以上就是今天要讲的内容,本文仅仅简单介绍了python是如何将照片马赛克化的,然后使用fastapi开发接口暴露给用户使用。