import subprocess
import cv2
import numpy as np
import time
def compare_image(image_1, image_2):
    start_time = time.time()
    first_image_hist = cv2.calcHist([image_1], [0], None, [256], [0, 256])
    second_image_hist = cv2.calcHist([image_2], [0], None, [256], [0, 256])
    img_hist_diff = cv2.compareHist(first_image_hist, second_image_hist, cv2.HISTCMP_BHATTACHARYYA)
    img_template_probability_match = cv2.matchTemplate(first_image_hist, second_image_hist, cv2.TM_CCOEFF_NORMED)[0][0]
    img_template_diff = 1 - img_template_probability_match
    commutative_image_diff = (img_hist_diff * 0.50) + img_template_diff
    end_time = time.time()
    return commutative_image_diff
# Replace the URLs with the actual file paths
show1_1 = cv2.imread("shows_iframes/wa3d/wa3d1.png")
show1_2 = cv2.imread("shows_iframes/wa3d/wa3d2.png")
show1_3 = cv2.imread("shows_iframes/wa3d/wa3d3.png")
show1_4 = cv2.imread("shows_iframes/wa3d/wa3d4.png")
show2_1 = cv2.imread("shows_iframes/INFO_SOIR/info1.png")
show2_2 = cv2.imread("shows_iframes/INFO_SOIR/info2.png")
show2_3 = cv2.imread("shows_iframes/INFO_SOIR/info3.png")
show2_4 = cv2.imread("shows_iframes/INFO_SOIR/info4.png")
show3_1 = cv2.imread("shows_iframes/AL_MASSAIYA/AL_MASSAIYA1.png")
show3_2 = cv2.imread("shows_iframes/AL_MASSAIYA/AL_MASSAIYA2.png")
show3_3 = cv2.imread("shows_iframes/AL_MASSAIYA/AL_MASSAIYA3.png")
show3_4 = cv2.imread("shows_iframes/AL_MASSAIYA/AL_MASSAIYA4.png")
reference_iframes = {
    "show1": [show1_1, show1_2, show1_3,show1_4],
    "show2": [show2_1, show2_2, show2_3,show2_4],
    "show3": [show3_1, show3_2, show3_3,show3_4],
}
# Construct the FFmpeg command
live_stream_url = "test.ts" #"http://your-live-stream-url/playlist.m3u8"
command = [
    "ffmpeg",
    "-i", live_stream_url,
    "-vf", "select='eq(pict_type,PICT_TYPE_I)'",
    "-vsync", "vfr",
    "-q:v", "2",
    "-f", "image2pipe",
    "-vcodec", "mjpeg",
    "-"
]
# Run the command and create a pipe to read the output
ffmpeg_process = subprocess.Popen(command, stdout=subprocess.PIPE)
while True:
    # Read the next iframe from the pipe
    iframe = ffmpeg_process.stdout.read(921654)  # Assuming a 640x480 image size, adjust this number accordingly
    #print('the iframe is :',iframe)
    if not iframe:
        break
    # Convert the JPEG data to an OpenCV image
    #iframe_image = cv2.imdecode(np.frombuffer(iframe, dtype=np.uint8), cv2.IMREAD_COLOR)
    iframe_image = cv2.imdecode(np.frombuffer(iframe, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
    
    
    print('the iframe is :',iframe_image)

    # Compare the extracted iframe with the reference iframes
    min_diff = 0.5 #float("inf")
    matched_show = None
    for show, show_iframes in reference_iframes.items():
        for ref_iframe in show_iframes:
            diff = compare_image(iframe_image, ref_iframe)
           # print('the first image is :',iframe_image,' the second image is :',ref_iframe)
            print('the image size is :',ref_iframe.shape)
            #print('the diff is :',diff)
            if diff < min_diff:
                min_diff = diff
                matched_show = show
    # Do something with the matched show
    print(f"Matched show: {matched_show}, diff: {min_diff}")