Spaces:
Running
Running
Foydalanuvchi commited on
Commit ·
5425774
1
Parent(s): d724deb
Phase 14: Ultra HD upgrade with Swin2SR AI and video detail boost
Browse files- filters.py +26 -6
- main.py +4 -0
filters.py
CHANGED
|
@@ -61,11 +61,33 @@ def apply_retro_filter(image_path, output_path):
|
|
| 61 |
return None
|
| 62 |
|
| 63 |
def upscale_image(image_path, output_path, scale=2):
|
| 64 |
-
"""Rasmni sifatini oshirish."""
|
| 65 |
try:
|
| 66 |
image_path = os.path.abspath(image_path)
|
| 67 |
output_path = os.path.abspath(output_path)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
img = cv2.imread(image_path)
|
| 70 |
if img is None: return None
|
| 71 |
|
|
@@ -80,13 +102,11 @@ def upscale_image(image_path, output_path, scale=2):
|
|
| 80 |
|
| 81 |
pill_img = Image.fromarray(cv2.cvtColor(upscaled, cv2.COLOR_BGR2RGB))
|
| 82 |
|
| 83 |
-
# O'
|
| 84 |
enhancer = ImageEnhance.Sharpness(pill_img)
|
| 85 |
-
pill_img = enhancer.enhance(2.
|
| 86 |
-
|
| 87 |
-
# Kontrastni ham biroz oshirish
|
| 88 |
enhancer_c = ImageEnhance.Contrast(pill_img)
|
| 89 |
-
pill_img = enhancer_c.enhance(1.
|
| 90 |
|
| 91 |
upscaled_final = cv2.cvtColor(np.array(pill_img), cv2.COLOR_RGB2BGR)
|
| 92 |
cv2.imwrite(output_path, upscaled_final)
|
|
|
|
| 61 |
return None
|
| 62 |
|
| 63 |
def upscale_image(image_path, output_path, scale=2):
|
| 64 |
+
"""Rasmni AI Super-Resolution (Swin2SR) orqali yoki lokal algoritm bilan sifatini oshirish."""
|
| 65 |
try:
|
| 66 |
image_path = os.path.abspath(image_path)
|
| 67 |
output_path = os.path.abspath(output_path)
|
| 68 |
|
| 69 |
+
# 1. Hugging Face AI orqali (Eng yuqori sifat)
|
| 70 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 71 |
+
if hf_token:
|
| 72 |
+
try:
|
| 73 |
+
from huggingface_hub import InferenceClient
|
| 74 |
+
client = InferenceClient(token=hf_token)
|
| 75 |
+
|
| 76 |
+
with open(image_path, 'rb') as f:
|
| 77 |
+
image_data = f.read()
|
| 78 |
+
|
| 79 |
+
logger.info("HF Upscale: AI Super-Resolution (Swin2SR) ishga tushirildi.")
|
| 80 |
+
# Swin2SR x2 modelini ishlatamiz
|
| 81 |
+
result = client.image_to_image(
|
| 82 |
+
image_data,
|
| 83 |
+
model="microsoft/swin2SR-classical-sr-x2-64",
|
| 84 |
+
)
|
| 85 |
+
result.save(output_path)
|
| 86 |
+
return output_path
|
| 87 |
+
except Exception as e:
|
| 88 |
+
logger.warning(f"HF Upscale xatosi (Lokalga o'tilmoqda): {e}")
|
| 89 |
+
|
| 90 |
+
# 2. Lokal algoritm (Tezkor va barqaror)
|
| 91 |
img = cv2.imread(image_path)
|
| 92 |
if img is None: return None
|
| 93 |
|
|
|
|
| 102 |
|
| 103 |
pill_img = Image.fromarray(cv2.cvtColor(upscaled, cv2.COLOR_BGR2RGB))
|
| 104 |
|
| 105 |
+
# O'tkirlik va kontrastni kuchaytirish
|
| 106 |
enhancer = ImageEnhance.Sharpness(pill_img)
|
| 107 |
+
pill_img = enhancer.enhance(2.8)
|
|
|
|
|
|
|
| 108 |
enhancer_c = ImageEnhance.Contrast(pill_img)
|
| 109 |
+
pill_img = enhancer_c.enhance(1.2)
|
| 110 |
|
| 111 |
upscaled_final = cv2.cvtColor(np.array(pill_img), cv2.COLOR_RGB2BGR)
|
| 112 |
cv2.imwrite(output_path, upscaled_final)
|
main.py
CHANGED
|
@@ -432,6 +432,10 @@ async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 432 |
success_path = await loop.run_in_executor(
|
| 433 |
executor, apply_style_transfer, input_path, output_path, style
|
| 434 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
else:
|
| 436 |
func = {
|
| 437 |
"r": apply_retro_filter, "u": upscale_image,
|
|
|
|
| 432 |
success_path = await loop.run_in_executor(
|
| 433 |
executor, apply_style_transfer, input_path, output_path, style
|
| 434 |
)
|
| 435 |
+
elif action == "u":
|
| 436 |
+
try: await query.edit_message_text("💎 AI Ultra HD sifatga oshirilmoqda...\n(Bu jarayon bir oz vaqt olishi mumkin)")
|
| 437 |
+
except: pass
|
| 438 |
+
success_path = await loop.run_in_executor(executor, upscale_image, input_path, output_path)
|
| 439 |
else:
|
| 440 |
func = {
|
| 441 |
"r": apply_retro_filter, "u": upscale_image,
|