|
import PIL.Image as Image |
|
import io |
|
from collections import Counter |
|
|
|
from .mamasnowflake import * |
|
|
|
|
|
|
|
def get_one_person(xyxy, |
|
img): |
|
crop = img[int(xyxy[0, 1]):int(xyxy[0, 3]), int(xyxy[0, 0]):int(xyxy[0, 2]), ::(-1)] |
|
result = output_to_binary(crop) |
|
return result |
|
|
|
|
|
|
|
def save_img_as_png(img, |
|
file_path, |
|
RGB=0): |
|
if RGB: |
|
Image.fromarray(img).save(file_path, format='PNG', quality=100, subsampling=0) |
|
else: |
|
Image.fromarray(img[..., ::-1]).save(file_path, format='PNG', quality=100, subsampling=0) |
|
|
|
|
|
|
|
def get_one_name(): |
|
|
|
snowflake = Snowflake(datacenter_id=1, worker_id=3) |
|
|
|
randID = snowflake.generate() |
|
randID = str(randID) |
|
file_name = f'{randID}.png' |
|
return file_name |
|
|
|
|
|
|
|
def extract_first_number(s): |
|
for char in s: |
|
if char.isdigit(): |
|
return int(char) |
|
return -1 |
|
|
|
|
|
|
|
def contains_any_keyword(string, keywords): |
|
for keyword in keywords: |
|
if keyword in string: |
|
return True |
|
return False |
|
|
|
|
|
|
|
def png_to_binary(file_path='../data/temp/temp.png'): |
|
with open(file_path, 'rb') as file: |
|
binary_data = file.read() |
|
return binary_data |
|
|
|
|
|
|
|
def output_to_binary(img_array): |
|
|
|
img = Image.fromarray(img_array) |
|
|
|
|
|
binary_data = io.BytesIO() |
|
img.save(binary_data, format='PNG') |
|
|
|
|
|
binary_data = binary_data.getvalue() |
|
|
|
return binary_data |
|
|
|
|
|
|
|
def calculate_loss(predictions, targets): |
|
|
|
function_loss = [abs(int(pred) - int(target)) for pred, target in zip(predictions, targets)] |
|
|
|
|
|
total_loss = sum(function_loss) |
|
|
|
|
|
avg_loss = total_loss / len(predictions) |
|
|
|
result = {'function_loss': function_loss, |
|
'total_loss': total_loss, |
|
'avg_loss': avg_loss, |
|
'total_person_num': len(predictions) |
|
} |
|
return result |
|
|
|
|
|
|
|
def count_words_in_strings(strings): |
|
word_counts = Counter() |
|
word_counts.update(strings) |
|
return dict(word_counts) |
|
|