Re bonjour à tous!
Je reviens vers vous car depuis j'ai bien avancé, l'appli c++ n'a pas changé mais j'ai terminé de faire mon site.
Finalement je l'ai fais avec un mélange de Photoshop + Dreamweaver + Webdev.
J'ai pris un hébergement pour pouvoir y stocker ce que je veux notamment les images de références (les crop) dont on se sert dans l'appli c++ (celles qui sont listé dans le csv.ext).
Sur mon serveur j'ai donc les images et le fichier csv.ext qui contient les url de ces images.
J'aimerai que dans mon code c++ soit lu le csv du serveur (avec l'url des images au lieu de leur chemin sur le disque dur du pc) mais là est le problème, dans le read_csv il est passé en paramètre "images" qui est une image pour lui (vector <> Mat) et bien sur ça ne marche pas des que je met une url dans le fichier csv à la place du chemin direct d'une image puisque pour lui une url n'est pas une image et puisqu'on accède pas de la même façon à une url qu'à une image. Mais je ne sais pas comment faire, une solution?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 int main(int argc, const char *argv[]) { // Check for valid command line arguments, print usage // if no arguments were given. /* if (argc != 4) { cout << "usage: " << argv[0] << " </path/to/haar_cascade> </path/to/csv.ext> </path/to/device id>" << endl; cout << "\t </path/to/haar_cascade> -- Path to the Haar Cascade for face detection." << endl; cout << "\t </path/to/csv.ext> -- Path to the CSV file with the face database." << endl; cout << "\t <device id> -- The webcam device id to grab frames from." << endl; exit(1); } // Get the path to your CSV: string fn_haar = string(argv[1]); string fn_csv = string(argv[2]); int deviceId = atoi(argv[3]); */ string fn_haar="D:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml"; string fn_csv = "D:\\opencv\\csv.ext"; int deviceId = 0; // These vectors hold the images and corresponding labels: vector<Mat> images; vector<int> labels; // Read in the data (fails if no valid input filename is given, but you'll get an error message): try { read_csv(fn_csv, images, labels); } catch (cv::Exception& e) { cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl; // nothing more we can do exit(1); } // Get the height from the first image. We'll need this // later in code to reshape the images to their original // size AND we need to reshape incoming faces to this size: int im_width = images[0].cols; int im_height = images[0].rows; // Create a FaceRecognizer and train it on the given images: Ptr<FaceRecognizer> model = createLBPHFaceRecognizer(); model->train(images, labels); // That's it for learning the Face Recognition model. You now // need to create the classifier for the task of Face Detection. // We are going to use the haar cascade you have specified in the // command line arguments: // CascadeClassifier haar_cascade; haar_cascade.load(fn_haar); Mat original; original= imread("D:\\hugh.jpg", CV_LOAD_IMAGE_COLOR); // Convert the current frame to grayscale: Mat gray; cvtColor(original, gray, CV_BGR2GRAY); // Find the faces in the frame: int prediction; vector< Rect_<int> > faces; vector<pair<vector<int>,vector< Rect_<int> >>> label; haar_cascade.detectMultiScale(gray, faces); vector<int> pred (faces.size()); // At this point you have the position of the faces in // faces. Now we'll get the faces, make a prediction and // annotate it in the video. Cool or what? for(unsigned int i = 0; i < faces.size(); i++) { nbFaces=faces.size(); // Process face by face: Rect face_i = faces[i]; //Tableau[i]=faces[i]; // Crop the face from the image. So simple with OpenCV C++: Mat face = gray(face_i); // Resizing the face is necessary for Eigenfaces and Fisherfaces. You can easily // verify this, by reading through the face recognition tutorial coming with OpenCV. // Resizing IS NOT NEEDED for Local Binary Patterns Histograms, so preparing the // input data really depends on the algorithm used. // // I strongly encourage you to play around with the algorithms. See which work best // in your scenario, LBPH should always be a contender for robust face recognition. // // Since I am showing the Fisherfaces algorithm here, I also show how to resize the // face you have just found: Mat face_resized; cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC); // Now perform the prediction, see how easy that is: prediction = model->predict(face_resized); pred[i]=prediction; // And finally write all we've found out to the original image! // First of all draw a green rectangle around the detected face: rectangle(original, face_i, CV_RGB(0, 255,0), 1); // Create the text we will annotate the box with: string box_text = format("Prediction = %d", prediction); // Calculate the position for annotated text (make sure we don't // put illegal values in there): int pos_x = max(face_i.tl().x - 10, 0); int pos_y = max(face_i.tl().y - 10, 0); // And now put it into the image: putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1, CV_RGB(0,255,0), 2); } label.push_back(make_pair(pred,faces)); imwrite("D:\\ImageScanée.jpg", original); namedWindow( "face_recognizer", 0 ); //Detect click on window and call mouseEvent cvSetMouseCallback("face_recognizer", mouseEvent, (void*)&label); // Show the result: imshow("face_recognizer", original); // And display it: waitKey(0); return 0; }
Merci à vous
EDIT:
En fouillant sur le net j'ai trouvé une librairie qui fait ce que je veux: Libcurl En gros elle convertit l'url d'une image en image.
Pour cela je passe donc en paramètre de ma fonction csv non plus un vector<mat> mais un vector <string> qui contient l'url de l'image. Cette url est ensuite convertie en image et mise dans un vector <Mat>.
Problème, le programme plante à l'éxécution "L'application n'a pas réussi à démarrer correctement (0xc0150002)". Après avoir regardé à quoi ça correspondait, il s'agirait d'une simple erreur de lien de dll, il en manque une, ou chose du genre...j'espère trouver ce qui bloque
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 static void read_csv(const string& filename, vector<string>& URLimages, vector<int>& labels, char separator = ';') { std::ifstream file(filename.c_str(), ifstream::in); if (!file) { string error_message = "No valid input file was given, please check the given filename."; CV_Error(CV_StsBadArg, error_message); } string line, path, classlabel; CURL *curl; CURLcode res; std::ostringstream stream; curl = curl_easy_init(); while (getline(file, line)) { stringstream liness(line); getline(liness, path, separator); getline(liness, classlabel); if(!path.empty() && !classlabel.empty()) { //******************************************************************************* //CONVERTIR URL EN IMAGE curl_easy_setopt(curl, CURLOPT_URL, URLimages); //the img url curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); // pass the writefunction curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream); // pass the stream ptr when the writefunction is called res = curl_easy_perform(curl); // start curl std::string output = stream.str(); // convert the stream into a string curl_easy_cleanup(curl); // cleanup std::vector<char> data = std::vector<char>( output.begin(), output.end() ); //convert string into a vector cv::Mat data_mat = cv::Mat(data); // create the cv::Mat datatype from the vector image = cv::imdecode(data_mat,1); //read an image from memory buffer //*********************************************************************************** image.push_back(imread(path, 0)); labels.push_back(atoi(classlabel.c_str())); } } }![]()
Partager