|
import cv2 |
|
import pytesseract |
|
import os |
|
import requests |
|
from flask import Flask, request, jsonify |
|
from queue import Queue |
|
from threading import Thread |
|
from io import BytesIO |
|
import numpy as np |
|
import urllib.request |
|
import requests |
|
import json |
|
|
|
app = Flask(__name__) |
|
image_queue = Queue() |
|
|
|
|
|
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe" |
|
|
|
|
|
def enhance_contrast(image): |
|
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) |
|
lab_planes = cv2.split(lab) |
|
clahe = cv2.createCLAHE(clipLimit=4.0, tileGridSize=(8, 8)) |
|
lab_planes = list(lab_planes) |
|
lab_planes[0] = clahe.apply(lab_planes[0]) |
|
lab = cv2.merge(lab_planes) |
|
enhanced_image = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR) |
|
return enhanced_image |
|
|
|
def extract_text_from_image(image): |
|
enhanced_image = enhance_contrast(image) |
|
gray = cv2.cvtColor(enhanced_image, cv2.COLOR_BGR2GRAY) |
|
text = pytesseract.image_to_string(gray) |
|
return text.strip() |
|
|
|
def process_image_result(initial_url, final_url, taskId, userId, type): |
|
if type == 'post-like': |
|
process_post_like(initial_url, final_url, taskId, userId) |
|
elif type == 'reel-like': |
|
process_reel_like(initial_url, final_url, taskId, userId) |
|
elif type == 'follow': |
|
process_follow_status(initial_url, final_url, taskId, userId) |
|
else: |
|
return |
|
|
|
|
|
image_queue.task_done() |
|
|
|
|
|
if not image_queue.empty(): |
|
|
|
next_image_urls = image_queue.get() |
|
|
|
if isinstance(next_image_urls, dict): |
|
|
|
next_initial_url = next_image_urls.get('initial_url') |
|
next_final_url = next_image_urls.get('final_url') |
|
next_taskId = next_image_urls.get('taskId') |
|
next_userId = next_image_urls.get('userId') |
|
next_type = next_image_urls.get('type') |
|
|
|
|
|
process_image_result(next_initial_url, next_final_url, next_taskId, next_userId, next_type) |
|
else: |
|
|
|
response_data = jsonify( |
|
{ |
|
"result":"All images processed" |
|
} |
|
) |
|
|
|
print(response_data.get_json()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
response_payload = json.dumps(response_data.get_json()) |
|
headers = {'Content-Type': 'application/json'} |
|
|
|
|
|
url = 'https://project-b-olive.vercel.app/api/ml/get-result' |
|
response = requests.post(url, data=response_payload, headers=headers) |
|
print(response.json()) |
|
|
|
|
|
|
|
def process_post_like(initial_url, final_url, taskId, userId): |
|
with app.app_context(): |
|
try: |
|
initial_image = urllib.request.urlopen(initial_url) |
|
final_image = urllib.request.urlopen(final_url) |
|
initial_np_arr = np.asarray(bytearray(initial_image.read()), dtype=np.uint8) |
|
final_np_arr = np.asarray(bytearray(final_image.read()), dtype=np.uint8) |
|
initial_screenshot = cv2.imdecode(initial_np_arr, cv2.IMREAD_COLOR) |
|
final_screenshot = cv2.imdecode(final_np_arr, cv2.IMREAD_COLOR) |
|
except Exception as e: |
|
return jsonify({'status': 'fail', 'code': 500, 'message': str(e)}), 500 |
|
|
|
|
|
roi_x = 0 |
|
roi_y = 0 |
|
roi_width = initial_screenshot.shape[1] |
|
roi_height = initial_screenshot.shape[0] |
|
|
|
|
|
initial_roi = initial_screenshot[int(roi_y * initial_screenshot.shape[0] / 100):int((roi_y + roi_height) * initial_screenshot.shape[0] / 100), |
|
int(roi_x * initial_screenshot.shape[1] / 100):int((roi_x + roi_width) * initial_screenshot.shape[1] / 100)] |
|
final_roi = final_screenshot[int(roi_y * final_screenshot.shape[0] / 100):int((roi_y + roi_height) * final_screenshot.shape[0] / 100), |
|
int(roi_x * final_screenshot.shape[1] / 100):int((roi_x + roi_width) * final_screenshot.shape[1] / 100)] |
|
|
|
initial_red_pixels = np.sum(initial_roi[:, :, 2] > 0) |
|
final_red_pixels = np.sum(final_roi[:, :, 2] > 0) |
|
|
|
if final_red_pixels > initial_red_pixels: |
|
result = "User liked the post!" |
|
user_liked = 1 |
|
elif final_red_pixels < initial_red_pixels: |
|
result = "User unliked the post." |
|
user_liked = 0 |
|
else: |
|
result = "No change in like status." |
|
user_liked = 0 |
|
|
|
|
|
|
|
response_data = jsonify( |
|
{ |
|
"taskId": taskId, |
|
"userId": userId, |
|
"result": user_liked |
|
} |
|
) |
|
|
|
print(response_data.get_json()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
response_payload = json.dumps(response_data.get_json()) |
|
headers = {'Content-Type': 'application/json'} |
|
|
|
|
|
url = 'https://project-b-olive.vercel.app/api/ml/get-result' |
|
response = requests.post(url, data=response_payload, headers=headers) |
|
print(response.json()) |
|
|
|
|
|
|
|
|
|
|
|
def process_reel_like(initial_url, final_url, taskId, userId): |
|
with app.app_context(): |
|
try: |
|
initial_image = urllib.request.urlopen(initial_url) |
|
final_image = urllib.request.urlopen(final_url) |
|
initial_np_arr = np.asarray(bytearray(initial_image.read()), dtype=np.uint8) |
|
final_np_arr = np.asarray(bytearray(final_image.read()), dtype=np.uint8) |
|
initial_screenshot = cv2.imdecode(initial_np_arr, cv2.IMREAD_COLOR) |
|
final_screenshot = cv2.imdecode(final_np_arr, cv2.IMREAD_COLOR) |
|
except Exception as e: |
|
return jsonify({'status': 'fail', 'code': 500, 'message': str(e)}), 500 |
|
|
|
|
|
roi_x = 0 |
|
roi_y = 75 |
|
roi_width = initial_screenshot.shape[1] |
|
roi_height =10 |
|
|
|
|
|
initial_roi = initial_screenshot[int(roi_y * initial_screenshot.shape[0] / 100):int((roi_y + roi_height) * initial_screenshot.shape[0] / 100), |
|
int(roi_x * initial_screenshot.shape[1] / 100):int((roi_x + roi_width) * initial_screenshot.shape[1] / 100)] |
|
final_roi = final_screenshot[int(roi_y * final_screenshot.shape[0] / 100):int((roi_y + roi_height) * final_screenshot.shape[0] / 100), |
|
int(roi_x * final_screenshot.shape[1] / 100):int((roi_x + roi_width) * final_screenshot.shape[1] / 100)] |
|
|
|
cv2.imshow("Initial ROI", initial_roi) |
|
|
|
cv2.waitKey(0) |
|
cv2.destroyAllWindows() |
|
|
|
initial_red_pixels = np.sum(initial_roi[:, :, 2] ) |
|
final_red_pixels = np.sum(final_roi[:, :, 2]) |
|
|
|
if final_red_pixels > initial_red_pixels: |
|
result = "User liked the post!" |
|
user_liked = 1 |
|
elif final_red_pixels < initial_red_pixels: |
|
result = "User unliked the post." |
|
user_liked = 0 |
|
else: |
|
result = "No change in like status." |
|
user_liked = 0 |
|
|
|
|
|
|
|
response_data = jsonify( |
|
{ |
|
"taskId": taskId, |
|
"userId": userId, |
|
"result": user_liked |
|
} |
|
) |
|
|
|
print(response_data.get_json()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
response_payload = json.dumps(response_data.get_json()) |
|
headers = {'Content-Type': 'application/json'} |
|
|
|
|
|
url = 'https://project-b-olive.vercel.app/api/ml/get-result' |
|
response = requests.post(url, data=response_payload, headers=headers) |
|
print(response.json()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_follow_status(initial_url, final_url, taskId, userId): |
|
with app.app_context(): |
|
try: |
|
initial_image_response = requests.get(initial_url) |
|
initial_image_np_arr = np.asarray(bytearray(initial_image_response.content), dtype=np.uint8) |
|
initial_image = cv2.imdecode(initial_image_np_arr, cv2.IMREAD_COLOR) |
|
|
|
final_image_response = requests.get(final_url) |
|
final_image_np_arr = np.asarray(bytearray(final_image_response.content), dtype=np.uint8) |
|
final_image = cv2.imdecode(final_image_np_arr, cv2.IMREAD_COLOR) |
|
except Exception as e: |
|
return jsonify({'status': 'fail', 'code': 500, 'message': str(e)}), 500 |
|
height, width, _ = initial_image.shape |
|
|
|
roi_x = 0 |
|
roi_y = 0 |
|
roi_width = width |
|
roi_height =30 |
|
|
|
|
|
initial_roi = initial_image[int(roi_y * height / 100):int((roi_y + roi_height) * height / 100), |
|
int(roi_x * width / 100):int((roi_x + roi_width) * width / 100)] |
|
final_roi = final_image[int(roi_y * height / 100):int((roi_y + roi_height) * height / 100), |
|
int(roi_x * width / 100):int((roi_x + roi_width) * width / 100)] |
|
|
|
|
|
initial_text = extract_text_from_image(initial_roi) |
|
final_text = extract_text_from_image(final_roi) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result = "" |
|
if "Follow" in initial_text and "Following" in final_text and "Following" not in initial_text: |
|
result = "User followed on Instagram!" |
|
user_followed = 1 |
|
elif "Following" in initial_text and "Follow" in final_text and "Following" not in final_text: |
|
result = "User unfollowed on Instagram!" |
|
user_followed = 0 |
|
else: |
|
result = "No change in follow status." |
|
user_followed = 0 |
|
|
|
response_data = jsonify({ |
|
"taskId": taskId, |
|
"userId": userId, |
|
"result": user_followed |
|
}) |
|
|
|
print(response_data.get_json()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
response_payload = json.dumps(response_data.get_json()) |
|
headers = {'Content-Type': 'application/json'} |
|
|
|
|
|
url = 'https://project-b-olive.vercel.app/api/ml/get-result' |
|
response = requests.post(url, data=response_payload, headers=headers) |
|
print(response.json()) |
|
|
|
|
|
|
|
|
|
@app.route('/receive-image', methods=['POST']) |
|
def receive_image(): |
|
initial_url = request.json['initial_url'] |
|
final_url = request.json['final_url'] |
|
taskId = request.json['taskId'] |
|
userId = request.json['userId'] |
|
type = request.json['type'] |
|
|
|
|
|
image_queue.put((initial_url, final_url, taskId, userId, type)) |
|
|
|
process_thread = Thread(target=process_image_result, args=(initial_url, final_url, taskId, userId, type)) |
|
process_thread.start() |
|
|
|
|
|
return jsonify({'status': 'success', 'code': 200, 'message': 'Images received', |
|
'data': {'received': 1, 'queue_position': image_queue.qsize(), 'task_id': taskId, 'userId': userId}}), 200 |
|
|
|
|
|
@app.errorhandler(404) |
|
def not_found_error(error): |
|
return jsonify({'status': 'error', 'code': 404, 'message': 'Resource not found'}), 404 |
|
|
|
@app.errorhandler(500) |
|
def internal_server_error(error): |
|
return jsonify({'status': 'fail', 'code': 500, 'message': 'Internal server error'}), 500 |
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |
|
|