Skip to content

Commit

Permalink
image ahsh
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoralcz committed Oct 18, 2021
1 parent 4d002a9 commit 93c4316
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
13 changes: 13 additions & 0 deletions app/api/handlers/imagehash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from PIL import Image
import imagehash
import requests
from io import BytesIO

def imagehashed(url: str):
response = requests.get(url)
buffer = response.content

im = Image.open(BytesIO(buffer))
avg_hash = imagehash.average_hash(im)

return { "hash": str(avg_hash)}
3 changes: 2 additions & 1 deletion app/api/routes/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from fastapi import APIRouter

from app.api.routes import upscale, nsfw_detector
from app.api.routes import upscale, nsfw_detector, hashes

router = APIRouter()
router.include_router(upscale.router, tags=["upscaler"], prefix="/upscales")
router.include_router(nsfw_detector.router, tags=["detector"], prefix="/detects")
router.include_router(hashes.router, tags=["hash"], prefix="/hash")
21 changes: 21 additions & 0 deletions app/api/routes/hashes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from fastapi import APIRouter, HTTPException, FastAPI
from app.api.handlers.imagehash import imagehashed
from typing import Optional
from pydantic import BaseModel
from starlette.responses import JSONResponse


class Image(BaseModel):
url: str


router = APIRouter()


# post because I don't want to deal with url
@router.post("/")
def hashes(image: Image):
result = imagehashed(image.url)
if result is None:
raise HTTPException(status_code=400, detail="url not found")
return JSONResponse(result)
3 changes: 1 addition & 2 deletions app/api/routes/nsfw_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Optional
from pydantic import BaseModel
from starlette.responses import JSONResponse
import io


class Image(BaseModel):
Expand All @@ -15,7 +14,7 @@ class Image(BaseModel):

# post because I don't want to deal with url
@router.post("/")
def upscale_url(image: Image):
def nsfw_detector(image: Image):
result = detect(image.url)
if result is None:
raise HTTPException(status_code=400, detail="url not found")
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ python-magic
requests
tensorflow
tensorflow-hub
imagehash

0 comments on commit 93c4316

Please sign in to comment.