Cant get the output after applying some processing on img


Im trying to build app that doing a detect and inpaint the code is totally tested worked fine on notebook
but when i try to to apply the same code on the script i cant see the output of the functions especially "bitwise_and "

Steps to reproduce

Code snippet:

import base64
import re
import uuid
from pathlib import Path

import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import spacy
import streamlit as st
import streamlit.components.v1 as components
import streamlit.components.v1 as stc
from keras.optimizers import Adam
from PIL import Image
from st_click_detector import click_detector

import itertools
import os
import random

import cv2
import cv2 as intpaint
import tensorflow as tf
from keras.models import Sequential
from keras.utils.np_utils import to_categorical
from PIL import Image, ImageChops, ImageEnhance
from pylab import *
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split

sns.set(style='white', context='notebook', palette='deep')
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'
#All Functions
def get_img_as_base64(file):
    with open(file, "rb") as f:
        data =
    return base64.b64encode(data).decode()
def make_grid(cols,rows):
    grid = [0]*cols
    for i in range(cols):
        with st.container():
            grid[i] = st.sidebar.columns(rows)
    return grid
def convert_to_ela_image(path, quality):
    temp_filename = 'temp_file_name.jpg'
    ela_filename = 'temp_ela.png'
    image ='RGB'), 'JPEG', quality = quality)
    temp_image =
    ela_image = ImageChops.difference(image, temp_image)
    extrema = ela_image.getextrema()
    max_diff = max([ex[1] for ex in extrema])
    if max_diff == 0:
        max_diff = 1
    scale = 255.0 / max_diff
    ela_image = ImageEnhance.Brightness(ela_image).enhance(scale)
    return ela_image
def prepare_image(image_path):
    image_size = (128, 128)
    return np.array(convert_to_ela_image(image_path, 90).resize(image_size)).flatten() / 255.0
def read_imgs(path):
    path = real
    for dirname, _, filenames in os.walk(path):
        for filename in filenames:
            if filename.endswith(('jpg', 'png','tif')):
                full_path = os.path.join(dirname, filename)
                if len(Y) % 500 == 0:
                    print(f'Processing {len(Y)} images')

    return X, Y
def plot_confusion_matrix(cm, classes,
                          title='Confusion matrix',
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
                 color="white" if cm[i, j] > thresh else "black")

    plt.ylabel('True label')
    plt.xlabel('Predicted label')

def apply_mask_to_image(image_path, uploaded_files):
    # Find mask image in the same directory as the input image
    mask_filename = os.path.splitext(image_path)[0] + ".mask.png"
    # st.write("mask file name:", mask_filename)
    # Iterate through the uploaded files and search for the desired mask file
    matching_file = None
    for uploaded_file in uploaded_files:
        if mask_filename.endswith(
            matching_file = uploaded_file
            # st.write("matching file:", matching_file)
            image =
            # st.image(image, caption='Mask Image', use_column_width=True)

    if matching_file is not None:
        # Load mask image
        original_image = np.array(image)
        mask = original_image
        return mask, matching_file
        return None, None

def show_images(org_img_path, mask, inpainted_img):
    org_img = plt.imread(org_img_path)
    org_img_bgr = cv2.cvtColor(org_img, cv2.COLOR_RGB2BGR)
    mask, _ = apply_mask_to_image(org_img_path)
    masked_img = cv2.bitwise_and(org_img_bgr, org_img_bgr, mask=cv2.bitwise_not(mask))
    st.image(org_img_bgr, channels="BGR", use_column_width=True)
    st.image(masked_img, channels="BGR", use_column_width=True)
    st.image(inpainted_img, channels="BGR", use_column_width=True)

def Inpaint(filepath):
    img =
    img = np.array(img)
    img = cv2.resize(img, None, fx=0.2, fy=0.2)
    mask, _ = apply_mask_to_image(filepath, file2)
        mask = cv2.resize(mask, (img.shape[1], img.shape[0]))
        _, mask1 = cv2.threshold(mask, 128, 255, cv2.THRESH_BINARY_INV)
        kernel = np.ones((10, 10), np.uint8)
        mask1=cv2.dilate(mask, kernel, iterations=1)
        mask1=cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
        mask1=cv2.medianBlur(mask, 5)
        st.text("Displaying mask image for debugging")
        st.image(mask1, caption='Distorted Image', channels="RGB")
        distort = cv2.bitwise_and(img,img, mask=mask1)   ##the issue happened here 
        st.text("Displaying distort image for debugging")
        st.image(distort, caption='Distorted Image', channels="RGB")
        show_images(filepath, mask, img)
        # st.text("Displaying distort image for debugging")
        # st.image(distort, caption='Distorted Image', channels="RGB")
        restored1 = img.copy()
        cv2.inpaint(distort, mask1, restored1, cv2.INPAINT_FSR_FAST)

        contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        for contour in contours:
            x, y, w, h = cv2.boundingRect(contour)
            color = (255, 0, 0)
            thickness = 5
            cv2.rectangle(restored1, (x, y), (x + w, y + h), color, thickness)
        st.error("No Mask Image found")

def load_model():
  return model
def Inference(Image, model):
    class_names = ['fake', 'real']
    image = prepare_image(Image)
    image = image.reshape(-1, 128, 128, 3)
    y_pred = model.predict(image)
    y_pred_class = np.argmax(y_pred, axis=1)[0]
    confidence = np.amax(y_pred) * 100
    ela_image = convert_to_ela_image(Image, 150)
    # Return the class name, confidence, and ELA image
    return class_names[y_pred_class], confidence, ela_image
Used Tools for this Project: OpenCV, TensorFlow, Keras, Pillow, Streamlit 

The source code is available on [GitHub]().
# img = get_img_as_base64("gedo.png")
page_bg_img = f"""
[data-testid="stAppViewContainer"] > .main {{ 
background-position: center;
# background-repeat: repeat;
background-attachment: local;
[data-testid="stToolbar"] {{
right: 2rem;
HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem">{}</div>"""
st.markdown(page_bg_img, unsafe_allow_html=True)
st.title("Fake Image Detector","\n")
add_select =
        "Choose one of the following:",
        ("Detector", "Inpainting")
st.sidebar.image("career/acu.png", use_column_width=True)
mygrid_logos = make_grid(4,4)
mygrid_logos[1][1].image("Streamlit.png",width=50, use_column_width=True)
mygrid_logos[1][2].image("tools/tfp.png",width=160 )
file2=st.file_uploader("", type=["png"],accept_multiple_files=True)
def main0():    
    file = st.file_uploader("", type=["jpg", "png", "jpeg", "jfif", "bmp", "tiff", "tif"])
    if file is not None:
    # Save the uploaded file to a temporary location
        with open(new, "wb") as f:
        # Get the file path of the temporary file
        file_path = new
    if add_select == "Detector":
        if file is None:
            st.text("Please upload an image file")
            # Load the selected image using Pillow
            image =
            st.image(image, use_column_width=True)
            # Call the Inference function to classify the image and get the confidence score
            image_class, confidence, ela_image = Inference(file, model)
            # Print the class and confidence score
            st.write("The image is classified as", image_class)
            st.write("The Confidence score is approximately", np.round(confidence,2))
    elif add_select == "Inpainting":
            image =
            st.image(image, use_column_width=True)
            # Call the Inference function to classify the image and get the confidence score
            image_class, confidence, ela_image = Inference(file, model)
            # Print the class and confidence score
            st.write("The image is classified as", image_class)
            st.write("The Confidence score is approximately", np.round(confidence,2))
            if image_class == 'fake':

                st.write("No Parts to be Inpainted The image is real")
if __name__ == '__main__':

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

**Expected behavior: Image, masked image, bound box on the inpainted parts.

Explain what you expect to happen when you run the code above.

**Actual behavior: in the first part as it check real or fake its fine but when i try to apply some preprocessing like filters using opencv i cant see its output on “bitwise_and” then the code go to except scope and not continue

Explain the undesired behavior or error you see when you run the code above.
If you’re seeing an error message, share the full contents of the error message here.

Debug info

  • Streamlit version: 1.11.0
  • Python version: 3.9.7
  • Using VS CODE
  • OS version:Windows 10 Pro 22H2 19045.2965
  • Browser version:Microsoft Edge
    Version 114.0.1823.37 (Official build) (64-bit)

Requirements file

Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.
Not sure what a requirements file is? Check out this doc and add a requirements file to your app.


  • Link to your GitHub repo:
  • Link to your deployed app:

Additional information

If needed, add any other context about the problem here.

Based on the code snippet and the description provided, it seems that there are a few issues in the code that might be causing the unexpected behavior. Here are some suggestions to address those issues:

  1. Check the imports: Make sure all the required libraries and modules are imported correctly. In the given code snippet, there are duplicate imports for some modules, such as streamlit.components.v1, which is imported twice.

  2. Check the bitwise_and operation: The code snippet doesn’t show the exact usage of bitwise_and operation, but if you’re not seeing the expected output, ensure that the inputs to the function are valid and correctly defined. Make sure the input images and masks have compatible dimensions.

  3. Handle exceptions properly: The code snippet includes a try-except block, but it doesn’t specify the type of exception being caught. It’s a good practice to handle specific exceptions or at least catch the general Exception class. Update the except block to print the exception or log an error message to help identify the issue.

  4. Verify the image paths: Ensure that the file paths provided to the functions, such as convert_to_ela_image, apply_mask_to_image, and show_images, are correct and accessible. You can print the file paths to verify if they point to the expected files.

  5. Use proper image formats: When using OpenCV functions, make sure the image formats are compatible. For example, when using cv2.cvtColor, ensure that the input image has the correct color space representation.

  6. Check image dimensions and resize: Ensure that the images have the correct dimensions expected by the model and other operations. Use the appropriate resizing functions to adjust the images’ sizes if needed.

  7. Verify mask images: In the function apply_mask_to_image, make sure the mask image is loaded correctly and has the same dimensions as the original image. Verify that the mask file is uploaded and recognized properly.

  8. Verify the execution flow: Check if the code execution reaches all the desired parts. You can add print statements or use a debugger to track the flow of the program and see which parts are executed.

By addressing these points, you should be able to identify and resolve the issues in your code. Remember to check for any error messages or stack traces that might provide additional information about the problem.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.