I am trying to take a picture with the camera and then detect the faces in it. But it doesn't work... The results array returns a count of zero. I have tested this code with a picture of somebody from the internet and it returned 1 found face. Here is my code:
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
Idea.CurrentIdea.idea.mockups.append(PFFile(data: UIImageJPEGRepresentation(pickedImage, 0.5)!))
//Face Detection
let cid:CIDetector = CIDetector(ofType:CIDetectorTypeFace, context:nil, options:[CIDetectorAccuracy: CIDetectorAccuracyHigh]);
let cii = CIImage(CGImage: pickedImage.CGImage!)
let results:NSArray = cid.featuresInImage(cii)
print(results.count)
for r in results {
let face:CIFaceFeature = r as! CIFaceFeature;
NSLog("Face found at (%f,%f) of dimensions %fx%f", face.bounds.origin.x, face.bounds.origin.y, face.bounds.width, face.bounds.height);
}
}
dismissViewControllerAnimated(true, completion: nil)
}
Any ideas? Thanks! There isn't much on the web about it recently.
Make sure that your image orientation and the expected image orientation for the detector are the same. See this answer for more detail: https://stackoverflow.com/a/17019107/919790
Related
I'm trying to save depth data from an iPad Pro's FaceId TrueDepth sensor. I have taken this demo code and have added the following code with a simple button:
#IBAction func exportData(_ sender: Any) {
let ciimage = CIImage(cvPixelBuffer: realDepthData.depthDataMap)
let depthUIImage = UIImage(ciImage: ciimage)
let data = depthUIImage.pngData()
print("data: \(realDepthData.depthDataMap)")
do {
let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
let path = directory.appendingPathComponent("FaceIdData.png");
try data!.write(to: path)
let activityViewController = UIActivityViewController(activityItems: [path], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = exportMeshButton
present(activityViewController, animated: true, completion: nil)
} catch {
print("Unable to save image")
}
}
realDepthData is a class property I added and that I update in dataOutputSynchronizer:
func dataOutputSynchronizer(_ synchronizer: AVCaptureDataOutputSynchronizer,
didOutput synchronizedDataCollection: AVCaptureSynchronizedDataCollection) {
...
let depthData = syncedDepthData.depthData
let depthPixelBuffer = depthData.depthDataMap
self.realDepthData = depthData
...
}
I'm able to save the image (grey scale) but I'm losing some depth information, notably in the background where all objects are fully white. You can see this in the image bellow, the wall and the second person behind are not correcly appearing (all white). If I'm not mistaken, from what I've seen in the app, I should have more information!
Thanks!
Only 32-bit depth makes sense – you can see image's depth setting its gamma. .exr and .hdr file formats support 32-bit. .png and .jpg are generally 8-bit. You should also consider channels order when converting.
I am new to swift, I am using swift 3.
I am trying to pick multiple image from photo library and I am using ELCimagepickercontroller
However when I am trying to read the images from the array I got error: Type 'Any' has no subscript members
My Code as below:
please let me know what's wrong
func elcImagePickerController(_ picker: ELCImagePickerController!, didFinishPickingMediaWithInfo info: [Any]!) {
self.dismiss(animated: true, completion: nil)
var i = 0
for item in info as [AnyObject]
{
i += 1
var imageview = UIImageView(image: (info[UIImagePickerControllerOriginalImage] as? [String]))
// var name = .uiImageJPEGRepresentation()!
}
}
Since the info parameter is an array of dictionaries, you need to properly cast item in your for loop.
func elcImagePickerController(_ picker: ELCImagePickerController, didFinishPickingMediaWithInfo info: [Any]) {
self.dismiss(animated: true, completion: nil)
for item in info as [String : Any]
{
if let image = item[UIImagePickerControllerOriginalImage] as? UIImage {
var imageview = UIImageView(image: image)
}
}
}
There were several other issues in your code. Don't needlessly add !. In fact, avoid all uses of ! until you fully comprehend their proper use. Until then, each use is a potential crash.
I am attempting to grab an NSURL from an image or video when a user selects it through a imagepickercontroller, using the Photos Framework
import Photos
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let url: NSURL = (info["UIImagePickerControllerReferenceURL"] as! NSURL)
mediaUploadView.image = info[UIImagePickerControllerOriginalImage]as? UIImage
print(url)
self.dismissViewControllerAnimated(true, completion: nil)
return UIImagePickerControllerReferenceURL
}
I receive the error "unexpected non void return value in void function".
The end goal is to use the NSURL to upload the image / video / gif to a Firebase Database.
I'm honestly not sure if my logic here is correct, I'm still new to coding. Any help is greatly appreciated!
In swift, you have to say in the at the end of the function before the opening "{" what the return value is going to be, if you don't, then it is considered a void function. A void function means that it does not return any value. So, to tell it that it will return a NSURL, try this:
import Photos
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) -> NSURL {
let url: NSURL = (info[UIImagePickerControllerReferenceURL] as! NSURL)
mediaUploadView.image = info[UIImagePickerControllerOriginalImage]as? UIImage
print(url)
self.dismissViewControllerAnimated(true, completion: nil)
return url
}
Just copy and paste this, the only difference is that it tells the function what the return value will be, and will no longer return that error. I also suggest removing the "" around the
(info[UIImagePickerControllerReferenceURL] as! NSURL)
This will return a NSURL.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imgProfileIcon.image=info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
print(info)
if let _ = imagePicked
{
if imagePicked.isEqualToData(UIImagePNGRepresentation(info[UIImagePickerControllerOriginalImage] as! UIImage)!)
{
print("U Picked The Same Image")
}
else
{
print("Different Image Picked")
}
}
imagePicked=UIImagePNGRepresentation(info[UIImagePickerControllerOriginalImage] as! UIImage)
isImagePicked=true
}
This is the delegate function that I use to select an image from The photoLibrary and my objective is to ensure that I don't hit a web service if an user selects the same image..... So while I was trying to compare images it always prints "Different Image Picked" Even though I pick the same image from the photo library.
I've tried all possibilities....even with .equal() but it shows the same... I think that some additional data is added due to which the comparison fails.
Can someone help me out?
How can I get the GPS coordinates of a UIImage after the picture was taken in a UIImagePickerController? Does the image contain the coordinates in it's metadata or should I create a class for the image and its coordinates and save that?
Thank you.
Please try to look at this and do as said in that answer..
I had done that and got success..
Hope It works for you..
Actually, you can access the metadata of an image right after you take the picture. The protocol method that is called by the system after an image has been selected is,
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
The NSDictionary argument, info, contains a key: UIImagePickerControllerMediaMetadata
AFAIK, You will need CLLocationManager to get the current location and geotag it.
From iOS 8 onwards you can use Photos Framework.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let url = info[UIImagePickerControllerReferenceURL] as? URL
if url != nil {
let fetchResult = PHAsset.fetchAssets(withALAssetURLs: [url!], options: nil)
let asset = fetchResult.firstObject
print(asset?.location?.coordinate.latitude)
print(asset?.creationDate)
}
}