|
@@ -23,33 +23,38 @@ from services.chaojiying import CJ
|
|
|
from services.defults import FAKE_USERS_DB
|
|
|
from services.limiter import limiter
|
|
|
from services.utils import calculate
|
|
|
+from services.zhipu import ZhipuFileExtract
|
|
|
|
|
|
images_router = APIRouter(prefix="/images")
|
|
|
|
|
|
|
|
|
-@images_router.post("/verify", summary="Ocr")
|
|
|
-async def simple_captcha(file: UploadFile = File(...)):
|
|
|
- start = time.time()
|
|
|
- img_bytes = await file.read()
|
|
|
- ocr = ddddocr.DdddOcr(det=False, ocr=True, show_ad=False)
|
|
|
- if img_bytes.startswith(b'data:image'):
|
|
|
- src = img_bytes.decode()
|
|
|
+def prepare_image(file):
|
|
|
+ if file.startswith(b'data:image'):
|
|
|
+ src = file.decode()
|
|
|
result = re.search("data:image/(?P<ext>.*?);base64,(?P<data>.*)", src, re.DOTALL)
|
|
|
if result:
|
|
|
# ext = result.groupdict().get("ext")
|
|
|
- img_base64 = result.groupdict().get("data")
|
|
|
+ return result.groupdict().get("data") # img_base64
|
|
|
else:
|
|
|
- raise Exception("Do not parse!")
|
|
|
- verify_code = ocr.classification(img_base64)
|
|
|
+ raise TypeError("transform image type failed!")
|
|
|
else:
|
|
|
- verify_code = ocr.classification(img_bytes)
|
|
|
+ assert isinstance(file, bytes)
|
|
|
+ return file # img_bytes
|
|
|
+
|
|
|
|
|
|
+@images_router.post("/verify", summary="Ocr")
|
|
|
+async def simple_captcha(file: UploadFile = File(...)):
|
|
|
+ start = time.time()
|
|
|
+ img_bytes = await file.read()
|
|
|
+ ocr = ddddocr.DdddOcr(det=False, ocr=True, show_ad=False)
|
|
|
+ image = prepare_image(img_bytes)
|
|
|
+ ret = ocr.classification(image)
|
|
|
return {
|
|
|
"msg": "success",
|
|
|
"code": 0,
|
|
|
"r": {
|
|
|
"time": float("{:.2f}".format(time.time() - start)),
|
|
|
- "code": verify_code
|
|
|
+ "code": ret
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -65,24 +70,14 @@ async def arithmetic_captcha(file: UploadFile = File(...)):
|
|
|
show_ad=False,
|
|
|
import_onnx_path=onnx_path,
|
|
|
charsets_path=charsets_path)
|
|
|
- if img_bytes.startswith(b'data:image'):
|
|
|
- src = img_bytes.decode()
|
|
|
- result = re.search("data:image/(?P<ext>.*?);base64,(?P<data>.*)", src, re.DOTALL)
|
|
|
- if result:
|
|
|
- img_base64 = result.groupdict().get("data")
|
|
|
- else:
|
|
|
- raise Exception("Do not parse!")
|
|
|
- verify_code = ocr.classification(img_base64)
|
|
|
- else:
|
|
|
- verify_code = ocr.classification(img_bytes)
|
|
|
-
|
|
|
- verify_code = verify_code.replace("x", "*")
|
|
|
+ image = prepare_image(img_bytes)
|
|
|
+ ret = image.replace("x", "*")
|
|
|
return {
|
|
|
"msg": "success",
|
|
|
"code": 0,
|
|
|
"r": {
|
|
|
"time": float("{:.2f}".format(time.time() - start)),
|
|
|
- "code": calculate(verify_code)
|
|
|
+ "code": calculate(ret)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -92,17 +87,13 @@ async def det_captcha(file: UploadFile = File(..., description="验证码图片"
|
|
|
det = ddddocr.DdddOcr(det=True, show_ad=False)
|
|
|
ocr = ddddocr.DdddOcr(ocr=True, show_ad=False)
|
|
|
img_bytes = await file.read()
|
|
|
- if img_bytes.startswith(b'data:image'):
|
|
|
- src = img_bytes.decode()
|
|
|
- result = re.search("data:image/(?P<ext>.*?);base64,(?P<data>.*)", src, re.DOTALL)
|
|
|
- if result:
|
|
|
- img_base64 = result.groupdict().get("data")
|
|
|
- else:
|
|
|
- raise Exception("Do not parse!")
|
|
|
- poses = det.detection(img_base64=img_base64)
|
|
|
- img_bytes = base64.b64decode(img_base64)
|
|
|
+ image = prepare_image(img_bytes)
|
|
|
+ if isinstance(image, bytes):
|
|
|
+ poses = det.detection(img_bytes=image)
|
|
|
else:
|
|
|
- poses = det.detection(img_bytes=img_bytes)
|
|
|
+ assert isinstance(image, str)
|
|
|
+ poses = det.detection(img_base64=image)
|
|
|
+ img_bytes = base64.b64decode(image)
|
|
|
|
|
|
img_byte = io.BytesIO(img_bytes)
|
|
|
file_array = np.frombuffer(img_byte.getbuffer(), np.uint8)
|
|
@@ -195,3 +186,27 @@ async def reset_limiter():
|
|
|
"code": 0,
|
|
|
"r": {}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+@images_router.post("/verify_z", summary="Ocr")
|
|
|
+async def zhipu_ocr(file: UploadFile = File(...)):
|
|
|
+ start = time.time()
|
|
|
+ img_bytes = await file.read()
|
|
|
+ image = prepare_image(img_bytes)
|
|
|
+
|
|
|
+ client = ZhipuFileExtract(
|
|
|
+ api_key="4abf8f53d1daed90f8a03fc982899418.dyK8ivPgzm3M1Nx5"
|
|
|
+ )
|
|
|
+ ret = client.glm_4v_flash(
|
|
|
+ image,
|
|
|
+ text='这是一张100以内数学计算图片验证码,图片内容计算结果是什么?你只需要返回图中内容计算结果即可,不要解释,不要返回任何其它内容'
|
|
|
+ )
|
|
|
+
|
|
|
+ return {
|
|
|
+ "msg": "success",
|
|
|
+ "code": 0,
|
|
|
+ "r": {
|
|
|
+ "time": float("{:.2f}".format(time.time() - start)),
|
|
|
+ "code": ret
|
|
|
+ }
|
|
|
+ }
|