public static Map
loadTrainData() throws Exception { Map
map = new HashMap
(); File dir = new File("train"); File[] files = dir.listFiles(); for (File file : files) { map.put(ImageIO.read(file), file.getName().charAt(0) + ""); } return map; }
In the attempt to use PHP to write captcha identification, refer to a Java code. But I don't know how to write in PHP when I encounter this.
The idea is to open the image in the directory, and then say the image as the map key, the file name to do the value.
Then I wrote the following code, but I didn't know what to do later.
public function loadTrainData() { $data = array(); $path = "train/train1/"; $handler = opendir($path); while (($filename = readdir($handler)) !== false) { if ($filename != "." && $filename != "..") { $files[] = file_get_contents($path.$filename); } } closedir($handler); #code 这里应该怎么写,还是说有更好的写法去写这个函数 return $data; }
Reply content:
public static Map
loadTrainData() throws Exception { Map
map = new HashMap
(); File dir = new File("train"); File[] files = dir.listFiles(); for (File file : files) { map.put(ImageIO.read(file), file.getName().charAt(0) + ""); } return map; }
In the attempt to use PHP to write captcha identification, refer to a Java code. But I don't know how to write in PHP when I encounter this.
The idea is to open the image in the directory, and then say the image as the map key, the file name to do the value.
Then I wrote the following code, but I didn't know what to do later.
public function loadTrainData() { $data = array(); $path = "train/train1/"; $handler = opendir($path); while (($filename = readdir($handler)) !== false) { if ($filename != "." && $filename != "..") { $files[] = file_get_contents($path.$filename); } } closedir($handler); #code 这里应该怎么写,还是说有更好的写法去写这个函数 return $data; }
is the train file name in your directory probably the same?A B C D ... 0 1 2 3
JavaThe code is to traverse the directory and then build one Map .
Your PHP code should look similar to this:
$files[$filename] = file_get_contents($path.$filename);
has been resolved.
public function loadTrainData() { $path = "train/tran1/"; $handler = opendir($path); while (($filename = readdir($handler)) !== false) { if ($filename != "." && $filename != "..") { $sampleMap[substr($filename, 0, 1)] = imagecreatefromjpeg($path.$filename); } } closedir($handler); return $sampleMap; }