Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can anyone help giving this code in Flutter #288

Open
md-rifatkhan opened this issue Jan 8, 2024 · 2 comments
Open

Can anyone help giving this code in Flutter #288

md-rifatkhan opened this issue Jan 8, 2024 · 2 comments

Comments

@md-rifatkhan
Copy link

md-rifatkhan commented Jan 8, 2024

Can anyone help giving this code in Flutter.

Shape: NHWC (Number, 512 (Height), 512 (Width), 3 (RGB Channel)
Python:

import cv2
import numpy as np
import tensorflow as tf
import time

# Load the TFLite model
interpreter = tf.lite.Interpreter(model_path="FF/realesr-general-x4v3_float32.tflite")
interpreter.allocate_tensors()

# Get input and output details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Load the input image
img = cv2.imread('FF/tnn.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Resize image to match model's expected sizing
img = cv2.resize(img, (input_details[0]['shape'][2], input_details[0]['shape'][1]))
img = np.expand_dims(img, axis=0)  # Add batch dimension

# Normalize the image
img = img.astype(np.float32) / 255

# Start time for measuring inference time
start_time = time.time()

# Set the tensor to point to the input data to be inferred
interpreter.set_tensor(input_details[0]['index'], img)

# Run inference
interpreter.invoke()

# Retrieve the model output
out_mat = interpreter.get_tensor(output_details[0]['index'])

# Measure and print elapsed time
elapsed_time = time.time() - start_time
print('Inference time: ', elapsed_time)

# Post-process and save output (as needed)
# Example: Convert output to desired format, visualize, etc.

# Example of saving the output
# Assuming the output is an image-like array
out_mat = (out_mat.squeeze() * 255).clip(0, 255).astype(np.uint8)
output_img = cv2.cvtColor(out_mat, cv2.COLOR_RGB2BGR)
cv2.imwrite('output.jpg', output_img)
print("Output image saved.")
@md-rifatkhan
Copy link
Author

Model link: drive

@md-rifatkhan
Copy link
Author

I can pay 2$ for coffee (usdt), if someone want :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant