How can I load camera preview instead of a image - android-bitmap

enter image description here
I'm Making an android app.
Well, this is a sample code for face detection provided by google.
Instead of using a jpeg file, I want to use my camera preview .
How should I change the code? It would be very thankful if you help me because I'm struggling with this for 3 hours.
ImageView myImageView = (ImageView) findViewById(R.id.imgview);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable=true;
Bitmap myBitmap = BitmapFactory.decodeResource(
getApplicationContext().getResources(),
R.drawable.test1,
options);

import cv2
import numpy as np
a = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml');
cam = cv2.VideoCapture(0);
while(True):
ret,img=cam.read();
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = a.detectMultiScale(gray,1.3,5)
for(x,y,w,h) in faces :
cv2.rectangle(img, (x,y), (x+w,y+h), (0,0,255), 2)
cv2.imshow("Face", img);
if(cv2.waitKey(1) == ord('q')):
break;
cam.release()
cv2.destroyAllWindows()

Related

React Native not able to draw image on canvas from file

I am having trouble getting a iOS asset file drawn to react-native-canvas.
I am loading the image with Expo Image Picker, and that is working (since it is working on the website as well)
Somewhere in this code, there is an exception being thrown, and I believe it is because the image src is not valid.
CanvasImage is imported from {Image} from react-native-canvas
#setImage2[0] is the current image, loaded from expo image picker, starts with file://, is a png
imageDraw = new CanvasImage(canv,setImage2[0].width,setImage2[0].height);
var imageinfo = await FileSystem.readAsStringAsync(setImage2[0].uri,{ encoding: FileSystem.EncodingType.Base64 });
imageDraw.src = "data:image/png;base64, "+imageinfo
imageDraw.height = setImage2[0].height
imageDraw.width = setImage2[0].width
var image_drawn = await can_context.drawImage(imageDraw,0,0,new_width,new_height);
const gID = await can_context.getImageData(0,0,new_width,new_height);
I am looking to draw the image to the canvas (context is can_context), and then get the pixels after drawing on the image.
Any help is greatly appreciated, even a different route to the same solution works.
Thanks in advance.

The right way to download images using selenium

I have read some articles that mentioned how to get image by selenium. For example:
from selenium import webdriver
import requests
driver=webdriver.Firefox()
driver.get("http/https://your website")
img=driver.find_element_by_xpath("xpath leading to your element")#locating element
src=img.get_attribute('src')#fetch the location of image
img=requests.get(src)#fetch image
with open('image.jpg','wb') as writer:#open for writing in binary mode
writer.write(img.content)#write the image
But does this methods have a risk of more bandwidth cost?
Is there any way just like I right click on the image and save as it to local PC?
I have tried use javascript to do that:
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
var img = document.getElementById('someImageId');
context.drawImage(img, 0, 0 );
var theData = context.getImageData(0, 0, img.width, img.height);
and meet cross-origin problem
Uncaught DOMException: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.
at <anonymous>:5:23
It's workaround is make another request just like what I do not want in the first line.
Any suggestions?
In order to avoid increasing network footprint you can consider the following approach:
Take the screenshot of the entire page using i.e. get_screenshot_as_png function
Get required element location and size
Extract "interesting" part of the page by cutting anything else but the required element's coordinates
Save the resulting file
Example code which stores the logo from the https://experitest.com/ site to logo.png file:
from selenium import webdriver
from PIL import Image
from io import BytesIO
options = webdriver.ChromeOptions()
options.add_argument("--kiosk")
driver = webdriver.Chrome(chrome_options=options)
driver.get('chrome://settings/')
driver.execute_script('chrome.settingsPrivate.setDefaultZoom(1.0);')
driver.get("https://experitest.com/")
element = driver.find_element_by_xpath("//a[#class='navbar-brand']/img")
location = element.location
size = element.size
png = driver.get_screenshot_as_png()
im = Image.open(BytesIO(png))
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
im = im.crop((left, top, right, bottom))
im.save('logo.png')
driver.quit()
Assumptions:
You have Pillow library installed (it should be as easy as pip install pillow command)
Your OS DPI scale level is set to 100%

Convert Vision face detection VNFaceLandmarkRegion2D points into frame coordinates to be scale

I'm using vision framework to detect face landmark and it's working fine but i need to transform the face landmarks like nose, eyes and for that i need to get nose, eyes position in frame coordinate as face landmark is drawing using VNFaceLandmarkRegion2D points.
Please let me know how to convert VNFaceLandmarkRegion2D points to frame coordinate. So i can get the location in view for transformation or suggest any other way to to transform face landmark.
This code from Joshua Newnham solves your problem.
func getTransformedPoints(
landmark:VNFaceLandmarkRegion2D,
faceRect:CGRect,
imageSize:CGSize) -> [CGPoint]{
// last point is 0.0
return landmark.normalizedPoints.map({ (np) -> CGPoint in
return CGPoint(
x: faceRect.origin.x + np.x * faceRect.size.width,
y: imageSize.height - (np.y * faceRect.size.height + faceRect.origin.y))
})
}
As a newbie this is what I could find to get face marks as a CGPoint:
First converted the selected image to CIImage
Used faceDetector on the image
Parsed the image for each face in case it has more than one
Code:
let chosenPicture = CIImage(data: (self.selectedimage.image?.tiffRepresentation)!)
let selectedFace = faceDetector?.features(in: chosenPicture!, options: [CIDetectorSmile:true])
for person in selectedFace as! [CIFaceFeature] {
let p1LeftEye = person.leftEyePosition
let p1RightEye = person.rightEyePosition
let p1Mouth = person.mouthPosition

How can I convert "rs2::video frame" to "CvCapture* "?

I'm newbie to the Intel Realsense SDK and coding in Visual Studio 2017(C or C++) for Intel Realsense camera D435.
In my example I have the following,
static rs2::frameset current_frameset;
auto color = current_frameset.get_color_frame();
frame = cvQueryFrame(color);
I've got an error on line 3 as "can not convert 'rs2::video_frame' to 'CvCapture'"
I've not being able to find a solution to this issue and it's proving difficult and resulted in more errors.
Does anyone know how I can overcome this problem?
Thanks for the help!
The cvQueryFrame accepts cvCapture instance, and it is used to retrieve the frame from camera. In LibRS, the frame you retrieved back can be used already, you don't have to get back it again. attached the snippet of CV example in LibRS, you can refer to the complete code here
rs2::pipeline pipe;
// Start streaming with default recommended configuration
pipe.start();
using namespace cv;
const auto window_name = "Display Image";
namedWindow(window_name, WINDOW_AUTOSIZE);
while (waitKey(1) < 0 && cvGetWindowHandle(window_name))
{
rs2::frameset data = pipe.wait_for_frames(); // Wait for next set of frames from the camera
rs2::frame depth = color_map(data.get_depth_frame());
// Query frame size (width and height)
const int w = depth.as<rs2::video_frame>().get_width();
const int h = depth.as<rs2::video_frame>().get_height();
// Create OpenCV matrix of size (w,h) from the colorized depth data
Mat image(Size(w, h), CV_8UC3, (void*)depth.get_data(), Mat::AUTO_STEP);
// Update the window with new data
imshow(window_name, image);
}

How do you resize images by batch on photoshop where it scales based on the smaller dimension

I dont really know how to explain this, but..
How do you resize images by batch on photoshop where it scales based on the smaller dimension.
So basically, i want images to resize to 200x200 and I want the image to take the smaller dimension, center the image, then crop the excess of the bigger dimension.
Is there a way to do this?
I hope I make sense.
This script will resize the image so that the smallest dimension will become 200 and then crop it to 200 x 200
app.preferences.rulerUnits = Units.PIXELS;
// call the source document
var srcDoc = app.activeDocument;
var imageWidth = srcDoc.width.value;
var imageHeight = srcDoc.height.value;
var ts = 200;
if (imageHeight < imageWidth)
{
// landscape
var h = 200;
var w = Math.floor((imageWidth/imageHeight)*h);
}
else
{
// portrait
var w = 200;
var h = Math.floor((imageHeight/imageWidth)*w);
}
srcDoc.resizeImage(w, h, 72, ResampleMethod.BICUBIC);
//crop it in the centre
app.activeDocument.resizeCanvas(ts, ts, AnchorPosition.MIDDLECENTER);
You may need to use Actions and Automate in order to accomplish this. Here is a link to a tutorial:
http://www.digitalartsonline.co.uk/tutorials/photoshop/how-resize-multiple-images-in-photoshop/
Go to Image > Image Size.
This will bring up the Image Size dialog box, as shown below:
see documentation