forked from photoprism/photoprism
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from kvalev/kvv-plugin-upscale
Upscaling and classification plugins
- Loading branch information
Showing
23 changed files
with
688 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: Build and archive plugin solibs | ||
|
||
on: | ||
push: | ||
branches: [ develop, preview ] | ||
pull_request: | ||
branches: [ develop, preview ] | ||
|
||
workflow_dispatch: | ||
|
||
jobs: | ||
build-and-archive-plugins: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Create compose stack | ||
run: docker-compose -f docker-compose.ci.yml up -d --build --force-recreate | ||
|
||
- name: Compile plugins | ||
id: compile_plugins | ||
run: docker-compose -f docker-compose.ci.yml exec -T photoprism make build-plugins | ||
|
||
- name: Look for plugin solibs | ||
run: docker-compose -f docker-compose.ci.yml exec -T photoprism find storage/plugins/ | ||
if: always() && steps.compile_plugins.outcome == 'success' | ||
|
||
- name: Copy plugins from container to host | ||
run: docker compose -f docker-compose.ci.yml cp photoprism:/go/src/github.com/photoprism/photoprism/storage/plugins/ plugins/ | ||
if: always() && steps.compile_plugins.outcome == 'success' | ||
|
||
- name: Tear down stack | ||
run: docker-compose -f docker-compose.ci.yml down | ||
|
||
- name: Archive plugins | ||
uses: actions/upload-artifact@v3 | ||
if: always() && steps.compile_plugins.outcome == 'success' | ||
continue-on-error: true | ||
with: | ||
name: plugins | ||
path: plugins/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
name: Build plugin docker images | ||
|
||
on: | ||
pull_request: | ||
paths: | ||
- "ext/**" | ||
|
||
jobs: | ||
publish-plugin-docker-images: | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
plugin: | ||
- name: realesrgan | ||
version: 0.3.0 | ||
- name: yolo8 | ||
version: 8.0.110 | ||
|
||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: dorny/paths-filter@v2 | ||
id: changes | ||
with: | ||
filters: | | ||
changed: 'ext/${{ matrix.plugin.name }}/**' | ||
- name: Checkout | ||
if: steps.changes.outputs.changed == 'true' | ||
uses: actions/checkout@v3 | ||
|
||
- name: Log in to Docker Hub | ||
if: steps.changes.outputs.changed == 'true' | ||
uses: docker/login-action@v1 | ||
with: | ||
username: ${{ secrets.DOCKER_USERNAME }} | ||
password: ${{ secrets.DOCKER_PASSWORD }} | ||
|
||
- name: Build and push Docker image | ||
if: steps.changes.outputs.changed == 'true' | ||
uses: docker/build-push-action@v2 | ||
with: | ||
context: ext/${{ matrix.plugin.name }} | ||
tags: kvalev/${{ matrix.plugin.name }}:${{ matrix.plugin.version }} | ||
push: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
version: '3.5' | ||
|
||
## FOR TEST AND DEVELOPMENT ONLY, DO NOT USE IN PRODUCTION ## | ||
## Setup: https://docs.photoprism.app/developer-guide/setup/ ## | ||
|
||
services: | ||
## PhotoPrism Development Environment | ||
photoprism: | ||
environment: | ||
PHOTOPRISM_PLUGIN_YOLO8_ENABLED: "true" | ||
PHOTOPRISM_PLUGIN_YOLO8_HOSTNAME: "yolo8" | ||
PHOTOPRISM_PLUGIN_YOLO8_PORT: "5000" | ||
PHOTOPRISM_PLUGIN_REALESRGAN_ENABLED: "true" | ||
PHOTOPRISM_PLUGIN_REALESRGAN_HOSTNAME: "realesrgan" | ||
PHOTOPRISM_PLUGIN_REALESRGAN_PORT: "5001" | ||
|
||
## Image classification API | ||
yolo8: | ||
image: kvalev/yolo8:8.0.110 | ||
pull_policy: always | ||
|
||
## Image upscaling API | ||
realesrgan: | ||
image: kvalev/realesrgan:0.3.0 | ||
pull_policy: always | ||
restart: always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
weights |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM python:3.10.11-slim-buster | ||
|
||
RUN apt-get update && apt-get install -y libgl1 libglib2.0-0 | ||
|
||
COPY requirements.txt /app/ | ||
RUN pip install -r /app/requirements.txt | ||
|
||
COPY . /app/ | ||
WORKDIR /app | ||
|
||
EXPOSE 5001 | ||
|
||
CMD ["python3", "main.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import base64 | ||
from io import BytesIO | ||
from basicsr.archs.rrdbnet_arch import RRDBNet | ||
from basicsr.utils.download_util import load_file_from_url | ||
from flask import Flask, request | ||
from realesrgan import RealESRGANer | ||
from realesrgan.archs.srvgg_arch import SRVGGNetCompact | ||
from PIL import Image | ||
import numpy as np | ||
import torch | ||
|
||
import logging | ||
import os | ||
|
||
app = Flask(__name__) | ||
|
||
REAL_ESRGAN_MODELS = { | ||
"realesr-general-x4v3": { | ||
"url": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth", | ||
"model_md5": "91a7644643c884ee00737db24e478156", | ||
"scale": 4, | ||
"model": lambda: SRVGGNetCompact( | ||
num_in_ch=3, | ||
num_out_ch=3, | ||
num_feat=64, | ||
num_conv=32, | ||
upscale=4, | ||
act_type="prelu", | ||
), | ||
}, | ||
"RealESRGAN_x4plus": { | ||
"url": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth", | ||
"model_md5": "99ec365d4afad750833258a1a24f44ca", | ||
"scale": 4, | ||
"model": lambda: RRDBNet( | ||
num_in_ch=3, | ||
num_out_ch=3, | ||
num_feat=64, | ||
num_block=23, | ||
num_grow_ch=32, | ||
scale=4, | ||
), | ||
}, | ||
} | ||
|
||
name = "realesr-general-x4v3" | ||
|
||
if name not in REAL_ESRGAN_MODELS: | ||
raise ValueError(f"Unknown RealESRGAN model name: {name}") | ||
|
||
model_info = REAL_ESRGAN_MODELS[name] | ||
|
||
model_path = os.path.join('weights', name + '.pth') | ||
if not os.path.isfile(model_path): | ||
ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
model_path = load_file_from_url(url=model_info["url"], model_dir=os.path.join(ROOT_DIR, 'weights'), progress=True, file_name=None) | ||
logging.info(f"RealESRGAN model path: {model_path}") | ||
|
||
device = "cuda" if torch.cuda.is_available() else "cpu" | ||
|
||
model = RealESRGANer( | ||
scale=model_info["scale"], | ||
model_path=model_path, | ||
model=model_info["model"](), | ||
half=True if "cuda" in device else False, | ||
device=device, | ||
) | ||
|
||
@app.route('/superscale', methods=['POST']) | ||
def superscale(): | ||
scale = request.json.get("scale", model_info["scale"]) | ||
|
||
image_data = base64.b64decode(request.json['image']) | ||
image = np.asarray(Image.open(BytesIO(image_data))) | ||
print(f"RealESRGAN input shape: {image.shape}, scale: {scale}", flush=True) | ||
|
||
upsampled = model.enhance(image, outscale=scale)[0] | ||
upsampled_img = Image.fromarray(upsampled) | ||
print(f"RealESRGAN output shape: {upsampled.shape}", flush=True) | ||
|
||
with BytesIO() as buffer: | ||
upsampled_img.save(buffer, format="jpeg") | ||
return {"image": base64.b64encode(buffer.getvalue()).decode()} | ||
|
||
if __name__ == '__main__': | ||
print("running") | ||
app.run(host='0.0.0.0', port=5001) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
realesrgan==0.3.0 | ||
flask==2.3.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.pt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM python:3.10.11-slim-buster | ||
|
||
RUN apt-get update && apt-get install -y libgl1 libglib2.0-0 | ||
|
||
COPY requirements.txt /app/ | ||
RUN pip install -r /app/requirements.txt | ||
|
||
COPY . /app/ | ||
WORKDIR /app | ||
|
||
EXPOSE 5000 | ||
|
||
CMD ["python3", "main.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import base64 | ||
from flask import Flask, request | ||
from io import BytesIO | ||
from PIL import Image | ||
from typing import List | ||
from ultralytics import YOLO | ||
from ultralytics.yolo.engine.results import Results | ||
|
||
app = Flask(__name__) | ||
|
||
model = "yolov8n" | ||
detect_model = YOLO(f"{model}.pt") | ||
classify_model = YOLO(f"{model}-cls.pt") | ||
|
||
@app.route('/hello', methods=['GET']) | ||
def hello(): | ||
return "elloh" | ||
|
||
@app.route('/classify', methods=['POST']) | ||
def classify(): | ||
image_data = base64.b64decode(request.json['image']) | ||
image = Image.open(BytesIO(image_data)) | ||
|
||
results: List[Results] = classify_model.predict(image) | ||
result = results[0] | ||
|
||
# take only the top3 results | ||
take = min(len(result.names), 3) | ||
top_n_idx = result.probs.argsort(0, descending=True)[:take].tolist() | ||
|
||
return {result.names[idx]: result.probs[idx].item() for idx in top_n_idx} | ||
|
||
@app.route('/detect', methods=['POST']) | ||
def detect(): | ||
image_data = base64.b64decode(request.json['image']) | ||
image = Image.open(BytesIO(image_data)) | ||
|
||
results: List[Results] = detect_model.predict(image) | ||
|
||
return results[0].tojson() | ||
|
||
if __name__ == '__main__': | ||
app.run(host='0.0.0.0', port=5000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ultralytics==8.0.110 | ||
flask==2.3.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.