Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Can I use that to make a groups of similar images from source array? #19

Open
vadimkklim opened this issue Jul 21, 2019 · 10 comments
Open

Comments

@vadimkklim
Copy link

No description provided.

@kuls14
Copy link

kuls14 commented Jul 25, 2019

@vadimkklim from what you want to make a group? If you can explain more I think I can help you.

@bizibizi
Copy link

bizibizi commented Jul 25, 2019

@kuls14 i am using "[[OSImageHashing sharedInstance] similarImagesWithHashingQuality:" to find similar images in photo library but it work only for copies of images. If images slightly different it will never find them. Maybe you can help how to setup accuracy?

I am requesting 100x100 imgage from PHImageManager, convert it to Data by "pngData" and apply "medium quality" from OSImageHashing

@bizibizi
Copy link

@kuls14 okay, i was trying also to convert image to 8x8 size, move it to grayscale but results became even badder

extension UIImage {
var noir: UIImage? {
    let context = CIContext(options: nil)
    guard let currentFilter = CIFilter(name: "CIPhotoEffectNoir") else { return nil }
    currentFilter.setValue(CIImage(image: self), forKey: kCIInputImageKey)
    if let output = currentFilter.outputImage,
        let cgImage = context.createCGImage(output, from: output.extent) {
        return UIImage(cgImage: cgImage, scale: scale, orientation: imageOrientation)
    }
    return nil
}
}

@bizibizi
Copy link

bizibizi commented Jul 26, 2019 via email

@bizibizi
Copy link

bizibizi commented Jul 26, 2019 via email

@kuls14
Copy link

kuls14 commented Jul 26, 2019

I am using this to get image from PHAssets

import Foundation
import Photos

extension PHAsset {
    /// To get Thumbnail image from PHAssets
    var image : UIImage {
        var thumbnail = UIImage()
        let imageManager = PHCachingImageManager()
        let options = PHImageRequestOptions()
        options.version = .current
        //        options.deliveryMode = .opportunistic
        let scale  = UIScreen.main.scale
        let size = CGSize(width: 50.0 * scale, height: 50.0 * scale)
        //                let size = CGSize(width: 100.0, height: 100.0)
        
        imageManager.requestImage(for: self, targetSize: size, contentMode: .aspectFill, options: nil) { (image, info) in
            thumbnail = image!
        }
        return thumbnail
    }
}

and for comparison I use

let similarImageIdsAsTuples = OSImageHashing.sharedInstance().similarImages(with: OSImageHashingQuality.high, forImages: images)

This will return tuples so I have use below method to get group of similar image in array so I can display it in tableview or collection view.

func findDuplicateImage() {
        var images: [OSTuple<NSString, NSData>] = []
        for i in 0..<arrAllImage.count {
            let imgData = arrAllImage[i].image.pngData()!
            images.append(OSTuple<NSString, NSData>(first: NSString(string: "\(i)"), andSecond: imgData as NSData))
        }
        
//
        let similarImageIdsAsTuples = OSImageHashing.sharedInstance().similarImages(with: OSImageHashingQuality.high, forImages: images)
        
        var arrayID = [[NSString]]()
        for tuple in similarImageIdsAsTuples {
            let id = [tuple.first!, tuple.second!]
            arrayID.append(id)
        }
        
        var resultArray = [[NSString]]()
        
        for (i,arrayI) in arrayID.enumerated() {
            DispatchQueue.main.async {
                self.lblNumberOfPhoto.text = R.string.localizable.processing_similar_photos_number("\(i)", "\(arrayID.count)")
            }
            
            if i == 0 {
                resultArray.append(arrayI)
            } else {
                var isContains = false
                for (j,result) in resultArray.enumerated() {
                    if result.contains(arrayI[1]) && result.contains(arrayI.first!) {
                        isContains = true
                        break
                    } else if result.contains(arrayI.first!)  {
                        var newdata = result
                        resultArray.remove(at: j)
                        newdata.append(arrayI[1])
                        resultArray.insert(newdata, at: j)
                        isContains = true
                        break
                    } else if result.contains(arrayI[1]) {
                        var newdata = result
                        resultArray.remove(at: j)
                        newdata.append(arrayI.first!)
                        resultArray.insert(newdata, at: j)
                        isContains = true
                        break
                    }
                }
                if !isContains {
                    resultArray.append(arrayI)
                }
            }
        }
        
        arrDupliImage.removeAll()
        
        for similarImageID in resultArray {
            var dupliImageTabel = [DuplicateImage]()
            
            for imgId in similarImageID {
                let data = arrAllImage.filter { (dupImg) -> Bool in
                    return dupImg.id == imgId.integerValue
                }
                if !data.isEmpty {
                    dupliImageTabel.append(data.first!)
                }
            }
            arrDupliImage.append(dupliImageTabel)
        }
}

Please try to use 150*150 as max size for image otherwise it may cause memory issue.

@bizibizi
Copy link

bizibizi commented Jul 26, 2019 via email

@kuls14
Copy link

kuls14 commented Jul 26, 2019

Yes smartClearner is definitely fast and more accurate for duplicate contacts too.

@bizibizi
Copy link

bizibizi commented Jul 26, 2019 via email

@akaraul
Copy link

akaraul commented Nov 9, 2020

@bizibizi Hey. did you find a better solution?

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

No branches or pull requests

4 participants