Esempio n. 1
0
def waveform(sound):
    startTime = request.args.get('startTime', '0')
    endTime = request.args.get('endTime', '0')
    if float(startTime) > float(endTime):
        endTime = startTime
    script = praat._scripts_dir + 'drawWave'
    params = [sound, startTime, endTime, praat._sounds_dir, praat._images_dir]

    ts = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S_")
    if "wav" not in sound:
        image = praat._images_dir + ts + str(sound.replace("mp3", "png"))
    else:
        image = praat._images_dir + ts + str(sound.replace("wav", "png"))

    # Add image name to params list
    params.append(image)

    # If image does not exist, run script
    #if not os.path.isfile(image):
    praat.runScript(script, params)
    utils.resizeImage(image, (1280, 640))
    #utils.cropImage(image)

    # Image should be available now, generated or cached
    with open(image) as fp:
        resp = app.make_response(fp.read())
    utils.rm_rf(image)
    resp.content_type = "image/png"
    return resp
Esempio n. 2
0
def drawElan(sound, startTime, endTime):

    #if(endTime<0)
    #resp = drawSound1(sound)

    showPitch = '0'

    # Script file
    script = praat._scripts_dir + "drawWaveform"

    # Parameters to the script
    params = [
        sound, startTime, endTime, showPitch, praat._sounds_dir,
        praat._images_dir
    ]

    # Image name will be a combination of relevant params joined by a period.
    if "wav" not in sound:
        image = praat._images_dir + str(sound.replace("mp3", "png"))
    else:
        image = praat._images_dir + str(sound.replace("wav", "png"))

    # Add image name to params list
    params.append(image)

    # If image does not exist, run script
    if not os.path.isfile(image):
        praat.runScript(script, params)
        utils.resizeImage(image)

    # Image should be available now, generated or cached
    resp = app.make_response(open(image).read())
    resp.content_type = "image/png"
    return resp
Esempio n. 3
0
def drawSound(sound, startTime, endTime):

    # Get URL parameters
    showSpectrogram = '0' if request.args.get("spectrogram") is None else '1'
    showPitch = '0' if request.args.get("pitch") is None else '1'
    showIntensity = '0' if request.args.get("intensity") is None else '1'
    showFormants = '0' if request.args.get("formants") is None else '1'
    showPulses = '0' if request.args.get("pulses") is None else '1'

    # Script file
    script = praat._scripts_dir + "drawSpectrogram"

    # Parameters to the script
    params = [
        sound, startTime, endTime, showSpectrogram, showPitch, showIntensity,
        showFormants, showPulses, praat._sounds_dir, praat._images_dir
    ]

    # Image name will be a combination of relevant params joined by a period.
    image = praat._images_dir + ".".join(params[:-2]) + ".png"

    # Add image name to params list
    params.append(image)

    # If image does not exist, run script
    if not os.path.isfile(image):
        praat.runScript(script, params)
        utils.resizeImage(image)

    # Image should be available now, generated or cached
    resp = app.make_response(open(image).read())
    resp.content_type = "image/png"
    return resp
Esempio n. 4
0
def drawSound1(sound):

    image = show_wave_n_spec(sound)

    print(image)
    utils.resizeImage(image)
    # Image should be available now, generated or cached
    resp = app.make_response(open(image).read())
    print(resp)
    #resp.content_type = "image/png"
    return send_file(image, mimetype='image/png')
Esempio n. 5
0
    def getComicPage(self, comic_id, page_number, cache = True, max_height = None):
        (path, page_count) = self.getSession().query(Comic.path, Comic.page_count) \
                                 .filter(Comic.id == int(comic_id)).first()

        image_data = None
        default_img_file = AppFolders.missingPath("page.png")
        
        if path is not None:
            if int(page_number) < page_count:
                if cache:
                    image_data = self.cache_load(comic_id,page_number,path)
                else:
                    ca = self.getComicArchive(comic_id,path)
                    # auto convert webp (disable for chunky or fix web book reader)
                    image_data = utils.webp_patch_convert(self.isBlacklist(ca.getPage(int(page_number))))
                    
        if image_data is None:
            with open(default_img_file, 'rb') as fd:
                image_data = fd.read()
            return image_data

        # resize image
        if max_height is not None:
            try:
                image_data = utils.resizeImage(int(max_height), image_data)
            except Exception as e:
                #logging.error(e)
                pass
                
        return image_data
Esempio n. 6
0
    def extractFaces(self, img, label=None, display=False):
        orig = cv2.imread(img, cv2.COLOR_BGR2GRAY)
        labels = []
        if self.verbose:
            print('Detecting image faces')
        if label is not None:
            faces, labels = utils.readExtractedFacesFile(label)
        else:
            faces = self.face_cascade.detectMultiScale(
                orig,
                scaleFactor=1.1,
                minNeighbors=5,
                minSize=config.faceMinSize,
                flags=cv2.CASCADE_SCALE_IMAGE)
        #if self.verbose:
        print(str(len(faces)) + ' faces detected')

        if display:
            for (x, y, w, h) in faces:
                cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 255, 0), 8)
            orig = utils.resizeImage(orig, 800)
            cv2.imshow('img', orig)
            cv2.waitKey(0)

        return faces, labels
Esempio n. 7
0
    def getComicPage(self, comic_id, page_number, max_height = None):
        (path, page_count) = self.getSession().query(Comic.path, Comic.page_count) \
                                 .filter(Comic.id == int(comic_id)).first()

        image_data = None
        default_img_file = AppFolders.imagePath("default.jpg")

        if path is not None:
            if int(page_number) < page_count:
                ca = self.getComicArchive(path)
                image_data = ca.getPage(int(page_number))

        if image_data is None:
            with open(default_img_file, 'rb') as fd:
                image_data = fd.read()
            return image_data

        # resize image
        if max_height is not None:
            try:
                image_data = utils.resizeImage(int(max_height), image_data)
            except Exception as e:
                #logging.error(e)
                pass
        return image_data
Esempio n. 8
0
    def getComicPage(self, comic_id, page_number, cache=True, max_height=None):
        (path, page_count) = self.getSession().query(Comic.path, Comic.page_count) \
                                 .filter(Comic.id == int(comic_id)).first()

        image_data = None
        default_img_file = AppFolders.missingPath("page.png")

        if path is not None:
            if int(page_number) < page_count:
                if cache:
                    image_data = self.cache_load(comic_id, page_number, path)
                else:
                    ca = self.getComicArchive(comic_id, path)
                    # auto convert webp (disable for chunky or fix web book reader)
                    image_data = utils.webp_patch_convert(
                        self.isBlacklist(ca.getPage(int(page_number))))

        if image_data is None:
            with open(default_img_file, 'rb') as fd:
                image_data = fd.read()
            return image_data

        # resize image
        if max_height is not None:
            try:
                image_data = utils.resizeImage(int(max_height), image_data)
            except Exception as e:
                #logging.error(e)
                pass

        return image_data
Esempio n. 9
0
    def getComicPage(self, comic_id, page_number, max_height=None):
        (path, page_count) = self.getSession().query(Comic.path, Comic.page_count) \
                                 .filter(Comic.id == int(comic_id)).first()

        image_data = None
        default_img_file = AppFolders.imagePath("default.jpg")

        if path is not None:
            if int(page_number) < page_count:
                ca = self.getComicArchive(path)
                image_data = ca.getPage(int(page_number))

        if image_data is None:
            with open(default_img_file, 'rb') as fd:
                image_data = fd.read()
            return image_data

        # resize image
        if max_height is not None:
            try:
                image_data = utils.resizeImage(int(max_height), image_data)
            except Exception as e:
                #logging.error(e)
                pass
        return image_data
    def evaluateEuclideanDistances(self, images):
        # Extract image features from each images in training set

        for i in range(len(images)):
            img = images[i]
            #if self.extract_features:
            #    self.processImage(img, True, self.verbose, self.heat_map)

            print(img)
            filename = utils.getFilename(img)

            #Reads IMs
            first_map = cv2.imread(config.maps_folder + filename +
                                   '_gge_map_0_1.png')
            second_map = cv2.imread(config.maps_folder + filename +
                                    '_gge_map_1_1.png')

            #first_map = self.resizeImage(first_map, 200)
            #second_map = self.resizeImage(second_map, 200)
            if first_map is None or second_map is None:
                continue

            gge_b, gge_g, gge_r = cv2.split(first_map)
            iic_b, iic_g, iic_r = cv2.split(second_map)
            #Get maps dimensions
            rows, cols, _ = first_map.shape
            #Building heat map
            heat_map = np.sqrt(
                pow(
                    gge_b[0:rows - 1, 0:cols - 1] -
                    iic_b[0:rows - 1, 0:cols - 1], 2) + pow(
                        gge_g[0:rows - 1, 0:cols - 1] -
                        iic_g[0:rows - 1, 0:cols - 1], 2) + pow(
                            gge_r[0:rows - 1, 0:cols - 1] -
                            iic_r[0:rows - 1, 0:cols - 1], 2))
            #Recover heat map max value
            max_value = np.ndarray.max(heat_map)
            #Normalization
            heat_map = heat_map / max_value
            sum = np.sum(heat_map)

            if self.heat_map:
                heat_map = heat_map * 255
                heat_map = heat_map.astype(np.uint8)
                #Display color map
                color_map = cv2.applyColorMap(heat_map, cv2.COLORMAP_JET)
                cv2.imshow('img', utils.resizeImage(color_map, 500))
                cv2.waitKey(0)

            print(sum)
    def detect(self, img, groundtruth=True):
        self.filename = utils.getFilename(img)
        print('Processing ' + self.filename)

        # Reads image
        image = cv2.imread(img)
        if image is None:
            print('Error processing ' + self.filename + ': image not found')
            return

        image = utils.resizeImage(image, 1200)

        # Mask
        if groundtruth:
            maskImage = cv2.imread(
                config.masks_folder + self.filename + '.png',
                cv2.IMREAD_GRAYSCALE)
            if maskImage is not None:
                maskImage = np.invert(maskImage)
                maskImage = utils.resizeImage(maskImage, 1200)
        else:
            maskImage = None

        if image is not None:

            self.evaluateIlluminantMaps(image, maskImage)
            rows, cols, _ = image.shape
            detectionMap = np.zeros((rows, cols))

            global_references = self.extractGlobalReferences(img)

            # Evaluate distances
            medians = {}
            for i in range(self.verticalBands):
                filename = config.temp_folder + 'vertical_band_' + str(
                    i) + "_gge_map_"
                for alg in self.algorithms:
                    medians[alg] = utils.evaluateRGBMedian(filename + alg +
                                                           ".png")

                band = self.bands['vertical'][i]
                band_feature = []

                for alg in self.algorithms:
                    if config.referenceColorType == 'median':
                        distance = utils.euclideanDistanceRGB(
                            medians[alg], self.verticalReferences[alg])
                    elif config.referenceColorType == 'global':
                        distance = utils.euclideanDistanceRGB(
                            medians[alg], global_references[alg])
                    band_feature.append(distance)

                    if config.regionalTrainingType is None:
                        detectionMap = band.incrementDetection(
                            detectionMap, distance)

                if config.regionalTrainingType == 'svm':
                    clf_type = 'vertical'
                    if config.referenceColorType == 'global':
                        clf_type += '_global'

                    prediction = self.clf[clf_type].predict_proba(
                        np.asarray(band_feature).reshape((1, -1)))
                    detectionMap = band.incrementDetection(
                        detectionMap, prediction[0][1])

                print('\tEvaluated ' + str(i + 1) + '/' +
                      str(self.verticalBands) + ' vertical bands')

            for i in range(self.horizontalBands):
                filename = config.temp_folder + 'horizontal_band_' + str(
                    i) + "_gge_map_"
                for alg in self.algorithms:
                    medians[alg] = utils.evaluateRGBMedian(filename + alg +
                                                           ".png")

                band = self.bands['horizontal'][i]
                band_feature = []

                for alg in self.algorithms:
                    if config.referenceColorType == 'median':
                        distance = utils.euclideanDistanceRGB(
                            medians[alg], self.verticalReferences[alg])
                    elif config.referenceColorType == 'global':
                        distance = utils.euclideanDistanceRGB(
                            medians[alg], global_references[alg])
                    band_feature.append(distance)

                    if config.regionalTrainingType is None:
                        detectionMap = band.incrementDetection(
                            detectionMap, distance)

                if config.regionalTrainingType == 'svm':
                    clf_type = 'horizontal'
                    if config.referenceColorType == 'global':
                        clf_type += '_global'

                    prediction = self.clf[clf_type].predict_proba(
                        np.asarray(band_feature).reshape((1, -1)))
                    detectionMap = band.incrementDetection(
                        detectionMap, prediction[0][1])

                print('\tEvaluated ' + str(i + 1) + '/' +
                      str(self.horizontalBands) + ' horizontal bands')

            # Recover detection map max value
            splicedPercent = len(
                np.where(detectionMap > config.fakeThreshold)
                [0]) / detectionMap.size
            print('Spliced area (%): ' + str(splicedPercent))

            if splicedPercent > config.splicedTolerance:
                print('Image is SPLICED - Score: ' + str(splicedPercent))
                outputMask = detectionMap.copy()
                outputMask[outputMask < config.fakeThreshold] = 0
                outputMask[outputMask >= config.fakeThreshold] = 1

                max_value = np.ndarray.max(detectionMap)
                detectionMap = detectionMap / max_value
                detectionMap *= 255

                if self.display_result:
                    # Display color map
                    color_map = detectionMap
                    color_map = color_map.astype(np.uint8)
                    color_map = cv2.applyColorMap(color_map, cv2.COLORMAP_JET)
                    out = np.concatenate((utils.resizeImage(
                        image, 500), utils.resizeImage(color_map, 500)),
                                         axis=1)
                    cv2.imshow('output', out)
                    cv2.waitKey(0)

                    # Display spliced regions
                    regionMask = np.zeros(image.shape, 'uint8')
                    regionMask[..., 0] = outputMask.copy()
                    regionMask[..., 1] = outputMask.copy()
                    regionMask[..., 2] = outputMask.copy()
                    splicedRegions = np.multiply(image, regionMask)
                    out = np.concatenate((utils.resizeImage(
                        image, 500), utils.resizeImage(splicedRegions, 500)),
                                         axis=1)
                    cv2.imshow('output', out)
                    cv2.waitKey(0)

                # Write output mask
                outputMask *= 255
                cv2.imwrite(config.regionOutputDetectionImage, outputMask)

            else:
                print('Image is ORIGINAL - Score: ' + str(splicedPercent))

        else:
            print('No image found: ' + img)
Esempio n. 12
0
def processImage():
    print("\n_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_")
    canvas = Canvas(back, width=r.winfo_width(), height=r.winfo_height(), highlightthickness=0, bg='black')
    canvas.pack()
    imgfile = utils.getFile()
    print("Processing: ", imgfile)
    
    try:
        print("Resizing...")
        img = utils.resizeImage(imgfile)
        print("Resize successful.")
    except Exception as e:
        print(type(e).__name__ + ":\n", e.args)
        canvas.create_text((r.winfo_width()/2), (r.winfo_height()/2),
            anchor='center',
            font=("Consolas", 16),
            fill='green',
            text='Error: This filetype is not supported.\nPlease reset and try again.',
            justify='center')
        raise
    
    try:
        print("Preparing for ASCII Conversion...")
        optimizedImg = optimize.optimizeImage(img)
        print("Ready to convert.")
    except Exception as e:
        canvas.create_text((r.winfo_width()/2), (r.winfo_height()/2),
            anchor='center',
            font=("Consolas", 16),
            fill='green',
            text='Something went wrong.\nPlease reset and try again.',
            justify='center') 
        raise
    # get dimensions & resolution
    w, h = optimizedImg.width, optimizedImg.height
    res = w * h

        ### Use this to simplify the JPEG ###
    # optimizedImg.save("optimizedImg.jpeg", 'JPEG', quality=150) 
    # optimizedImg = Image.open("optimizedImg.jpeg")

    px = optimizedImg.load()

        ### Use this in conjunction with above option to delete the copy it makes ###
    # import os
    # os.remove("optimizedImg.jpeg")
    try:
        asciiImg = convert.convert(w,h,px)
        print("Finished processing successfully!")
    except Exception as e:
        canvas.create_text((r.winfo_width()/2), (r.winfo_height()/2),
            anchor='center',
            font=("Consolas", 16),
            fill='green',
            text='Something went wrong.\nPlease reset and try again.',
            justify='center') 
        raise

    canvas.create_text((r.winfo_width()/2), ((r.winfo_height()/2)-20), anchor='center', font=("Consolas", 4), fill='green', text=asciiImg)
    
    try:
        print("Saving .txt file...")
        utils.saveAsTxt(asciiImg)
    except Exception as e:
        canvas.create_text((r.winfo_width()/2), (r.winfo_height()/2),
            anchor='center',
            font=("Consolas", 16),
            fill='green',
            text='Something went wrong.\nPlease reset and try again.',
            justify='center') 
        raise
    print("Process complete.")
    print("_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n")
Esempio n. 13
0
    def detect(self, img, detected_faces=None):
        filename = utils.getFilename(img)

        config.maps_folder = config.temp_folder
        config.faces_folder = config.temp_folder
        config.descriptors_folder = config.temp_folder

        print('Processing ' + filename)

        image = cv2.imread(img)
        if image is None:
            print('ERROR processing ' + filename + ': image not found')
            return -1

        #Check if image is colored
        try:
            _, _, channels = image.shape
            if channels < 3:
                raise Exception()
        except Exception as e:
            print(
                'ERROR: image is grayscale. Illuminant maps analysis cannot be performed.'
            )
            return -1

        #Loads classifier
        # Extracting image features
        # Extract the faces in the image
        faces, _ = self.extractFaces(img, detected_faces)

        #Prediction map
        predictions = {}
        counters = {}
        for i in range(len(faces)):
            predictions[i], counters[i] = 0, 0

        #Image is precessable if there are more than one image
        if len(faces) > 1:
            # If there are two or more faces, process the image
            # Extract maps
            self.extractIlluminationMaps(img)
            # Extract image descriptors and features
            for illum in config.illuminantTypes:
                for desc in self.descriptors:
                    clfPath = config.classification_folder + 'model_' + illum + '_' + desc.lower(
                    ) + '.pkl'
                    if os.path.isfile(clfPath):
                        clf = KNNClassifier.load(clfPath)
                        features = self.extractFeatures(img,
                                                        label=detected_faces,
                                                        faces=faces,
                                                        illum=illum,
                                                        descriptor=desc,
                                                        output=True)
                        #Predict over sample
                        for sample in features:
                            prediction = clf.predict(
                                np.array(sample.feature.split(),
                                         dtype=float).reshape((1, -1)),
                                True)[0][1]
                            predictions[sample.first] += prediction
                            predictions[sample.second] += prediction
                            counters[sample.first] += 1
                            counters[sample.second] += 1

            #Majority voting
            threshold = config.majorityVotingThreshold
            score = 0
            detected = False
            fakeFaces = []

            for i in predictions:
                if score < predictions[i] / counters[i]:
                    score = predictions[i] / counters[i]

                if predictions[i] / counters[i] > threshold:
                    fakeFaces.append(i)
                    print('\tFace ' + str(i + 1) + ' is FAKE. Score ' +
                          str(score))

                    if not detected:
                        detected = not detected
                else:
                    print('\tFace ' + str(i + 1) + ' is NORMAL. Score ' +
                          str(predictions[i] / counters[i]))

            if detected:
                print('Image is FAKE')
            else:
                print('Image is ORIGINAL')

            orig = cv2.imread(img, cv2.COLOR_BGR2GRAY)
            #Display spliced faces
            rows, cols, _ = orig.shape
            outputMask = np.zeros((rows, cols))

            faceScores = orig.copy()
            idx = 0
            font = cv2.FONT_HERSHEY_SIMPLEX
            for (x, y, w, h) in faces:
                face_score = predictions[idx] / counters[i]
                if idx not in fakeFaces:
                    cv2.rectangle(faceScores, (x, y), (x + w, y + h),
                                  (0, 255, 0), 8)
                    cv2.putText(faceScores, str("{:.3f}".format(face_score)),
                                (x, y + h + 80), font, 1.8, (0, 255, 0), 3)
                else:
                    cv2.rectangle(faceScores, (x, y), (x + w, y + h),
                                  (0, 0, 255), 8)
                    cv2.putText(faceScores, str("{:.3f}".format(face_score)),
                                (x, y + h + 80), font, 1.8, (0, 0, 255), 3)

                #Set score in detection map
                outputMask[y:y + h, x:x + w] = face_score
                idx += 1
            faceScores = utils.resizeImage(faceScores, 1000)

            if self.display_result:
                cv2.imshow('output', faceScores)
                cv2.waitKey()

            regionMask = np.zeros(orig.shape)
            regionMask[..., 0] = outputMask.copy()
            regionMask[..., 1] = outputMask.copy()
            regionMask[..., 2] = outputMask.copy()
            # Write output mask
            outputMask *= 255
            cv2.imwrite(config.faceOutputDetectionImage, outputMask)

            return score

        else:
            # discard the current image
            print('Not suitable number of faces found in the image')
            return -1
Esempio n. 14
0
def resizePic(basePath):
    list = os.listdir(basePath)
    for file in list:
        if file == '.DS_Store':
            continue
        util.resizeImage(basePath + file,[512,512,150],newFileName=basePath + file)
Esempio n. 15
0
def generateSplicedTrainingSet(direction):
    print('Loading color checked dataset')

    resized_width = 1200
    band_size = 200
    total = 100

    images = []
    labels = []
    files = os.listdir(config.imagesFolder)
    for i in files:
        try:
            img = config.imagesFolder + i
            if os.path.isfile(img) and not i.startswith('.') and i.startswith(
                    'normal'):
                images.append(img)
                labels.append(1)
        except:
            print("Error on processing image: " + i)
    print('Loaded color checker dataset')

    counter = 0
    for i in images:
        found = False
        while not found:
            randIdx = randint(0, len(images) - 1)
            if randIdx != counter:
                found = True

        host = cv2.imread(i)
        alien = cv2.imread(images[randIdx])

        host = utils.resizeImage(host, resized_width)
        resized_height, _, _ = host.shape
        alien = utils.resizeImage(alien, resized_width, resized_height)

        if direction == 'vertical':
            start = randint(1, resized_width / band_size - 1)
            start *= band_size

            end = start + band_size
            band = alien[:, start:end]
            host[:, start:end] = band

        elif direction == 'horizontal':
            found = False
            while not found:
                start = randint(0, int(resized_height / band_size) - 1)
                start *= band_size
                if start < resized_height - band_size - 1:
                    found = True

            end = start + band_size
            band = alien[start:end, :]
            host[start:end, :] = band

        path = config.output_spliced_dataset_folder + direction + '/'
        filename = path + direction + '_spliced_' + str(counter)
        cv2.imwrite(filename + '.png', host)
        np.savetxt(filename + '.txt', np.array([start, end, band_size]))

        counter += 1
        if counter == total:
            break